Greasy Fork镜像 还支持 简体中文。

node-lz4

LZ4 fast compression algorithm for js,clone from https://github.com/pierrec/node-lz4 upgrade Buffer.js to version 4.9.0 for fix new version Firefox.

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/24510/155728/node-lz4.js

  1. 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);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"./utils":[function(require,module,exports){
  2. /**
  3. * Javascript emulated bindings
  4. */
  5. var XXH = require('xxhashjs')
  6.  
  7. var CHECKSUM_SEED = 0
  8.  
  9. // Header checksum is second byte of xxhash using 0 as a seed
  10. exports.descriptorChecksum = function (d) {
  11. return (XXH(d, CHECKSUM_SEED).toNumber() >> 8) & 0xFF
  12. }
  13.  
  14. exports.blockChecksum = function (d) {
  15. return XXH(d, CHECKSUM_SEED).toNumber()
  16. }
  17.  
  18. exports.streamChecksum = function (d, c) {
  19. if (d === null)
  20. return c.digest().toNumber()
  21.  
  22. if (c === null)
  23. c = XXH(CHECKSUM_SEED)
  24.  
  25. return c.update(d)
  26. }
  27.  
  28. // Provide simple readInt32LE as the Buffer ones from node and browserify are incompatible
  29. exports.readInt32LE = function (buffer, offset) {
  30. return (buffer[offset]) |
  31. (buffer[offset + 1] << 8) |
  32. (buffer[offset + 2] << 16) |
  33. (buffer[offset + 3] << 24)
  34. }
  35.  
  36. exports.bindings = require('./binding')
  37.  
  38. },{"./binding":1,"xxhashjs":10}],1:[function(require,module,exports){
  39. /**
  40. Javascript version of the key LZ4 C functions
  41. */
  42. var uint32 = require('cuint').UINT32
  43.  
  44. if (!Math.imul) Math.imul = function imul(a, b) {
  45. var ah = a >>> 16;
  46. var al = a & 0xffff;
  47. var bh = b >>> 16;
  48. var bl = b & 0xffff;
  49. return (al*bl + ((ah*bl + al*bh) << 16))|0;
  50. };
  51.  
  52. /**
  53. * Decode a block. Assumptions: input contains all sequences of a
  54. * chunk, output is large enough to receive the decoded data.
  55. * If the output buffer is too small, an error will be thrown.
  56. * If the returned value is negative, an error occured at the returned offset.
  57. *
  58. * @param input {Buffer} input data
  59. * @param output {Buffer} output data
  60. * @return {Number} number of decoded bytes
  61. * @private
  62. */
  63. exports.uncompress = function (input, output, sIdx, eIdx) {
  64. sIdx = sIdx || 0
  65. eIdx = eIdx || (input.length - sIdx)
  66. // Process each sequence in the incoming data
  67. for (var i = sIdx, n = eIdx, j = 0; i < n;) {
  68. var token = input[i++]
  69.  
  70. // Literals
  71. var literals_length = (token >> 4)
  72. if (literals_length > 0) {
  73. // length of literals
  74. var l = literals_length + 240
  75. while (l === 255) {
  76. l = input[i++]
  77. literals_length += l
  78. }
  79.  
  80. // Copy the literals
  81. var end = i + literals_length
  82. while (i < end) output[j++] = input[i++]
  83.  
  84. // End of buffer?
  85. if (i === n) return j
  86. }
  87.  
  88. // Match copy
  89. // 2 bytes offset (little endian)
  90. var offset = input[i++] | (input[i++] << 8)
  91.  
  92. // 0 is an invalid offset value
  93. if (offset === 0 || offset > j) return -(i-2)
  94.  
  95. // length of match copy
  96. var match_length = (token & 0xf)
  97. var l = match_length + 240
  98. while (l === 255) {
  99. l = input[i++]
  100. match_length += l
  101. }
  102.  
  103. // Copy the match
  104. var pos = j - offset // position of the match copy in the current output
  105. var end = j + match_length + 4 // minmatch = 4
  106. while (j < end) output[j++] = output[pos++]
  107. }
  108.  
  109. return j
  110. }
  111.  
  112. var
  113. maxInputSize = 0x7E000000
  114. , minMatch = 4
  115. // uint32() optimization
  116. , hashLog = 16
  117. , hashShift = (minMatch * 8) - hashLog
  118. , hashSize = 1 << hashLog
  119.  
  120. , copyLength = 8
  121. , lastLiterals = 5
  122. , mfLimit = copyLength + minMatch
  123. , skipStrength = 6
  124.  
  125. , mlBits = 4
  126. , mlMask = (1 << mlBits) - 1
  127. , runBits = 8 - mlBits
  128. , runMask = (1 << runBits) - 1
  129.  
  130. , hasher = 2654435761
  131.  
  132. // CompressBound returns the maximum length of a lz4 block, given it's uncompressed length
  133. exports.compressBound = function (isize) {
  134. return isize > maxInputSize
  135. ? 0
  136. : (isize + (isize/255) + 16) | 0
  137. }
  138.  
  139. exports.compress = function (src, dst, sIdx, eIdx) {
  140. // V8 optimization: non sparse array with integers
  141. var hashTable = new Array(hashSize)
  142. for (var i = 0; i < hashSize; i++) {
  143. hashTable[i] = 0
  144. }
  145. return compressBlock(src, dst, 0, hashTable, sIdx || 0, eIdx || dst.length)
  146. }
  147.  
  148. exports.compressHC = exports.compress
  149.  
  150. exports.compressDependent = compressBlock
  151.  
  152. function compressBlock (src, dst, pos, hashTable, sIdx, eIdx) {
  153. var dpos = sIdx
  154. var dlen = eIdx - sIdx
  155. var anchor = 0
  156.  
  157. if (src.length >= maxInputSize) throw new Error("input too large")
  158.  
  159. // Minimum of input bytes for compression (LZ4 specs)
  160. if (src.length > mfLimit) {
  161. var n = exports.compressBound(src.length)
  162. if ( dlen < n ) throw Error("output too small: " + dlen + " < " + n)
  163.  
  164. var
  165. step = 1
  166. , findMatchAttempts = (1 << skipStrength) + 3
  167. // Keep last few bytes incompressible (LZ4 specs):
  168. // last 5 bytes must be literals
  169. , srcLength = src.length - mfLimit
  170.  
  171. while (pos + minMatch < srcLength) {
  172. // Find a match
  173. // min match of 4 bytes aka sequence
  174. var sequenceLowBits = src[pos+1]<<8 | src[pos]
  175. var sequenceHighBits = src[pos+3]<<8 | src[pos+2]
  176. // compute hash for the current sequence
  177. var hash = Math.imul(sequenceLowBits | (sequenceHighBits << 16), hasher) >>> hashShift
  178. // get the position of the sequence matching the hash
  179. // NB. since 2 different sequences may have the same hash
  180. // it is double-checked below
  181. // do -1 to distinguish between initialized and uninitialized values
  182. var ref = hashTable[hash] - 1
  183. // save position of current sequence in hash table
  184. hashTable[hash] = pos + 1
  185.  
  186. // first reference or within 64k limit or current sequence !== hashed one: no match
  187. if ( ref < 0 ||
  188. ((pos - ref) >>> 16) > 0 ||
  189. (
  190. ((src[ref+3]<<8 | src[ref+2]) != sequenceHighBits) ||
  191. ((src[ref+1]<<8 | src[ref]) != sequenceLowBits )
  192. )
  193. ) {
  194. // increase step if nothing found within limit
  195. step = findMatchAttempts++ >> skipStrength
  196. pos += step
  197. continue
  198. }
  199.  
  200. findMatchAttempts = (1 << skipStrength) + 3
  201.  
  202. // got a match
  203. var literals_length = pos - anchor
  204. var offset = pos - ref
  205.  
  206. // minMatch already verified
  207. pos += minMatch
  208. ref += minMatch
  209.  
  210. // move to the end of the match (>=minMatch)
  211. var match_length = pos
  212. while (pos < srcLength && src[pos] == src[ref]) {
  213. pos++
  214. ref++
  215. }
  216.  
  217. // match length
  218. match_length = pos - match_length
  219.  
  220. // token
  221. var token = match_length < mlMask ? match_length : mlMask
  222.  
  223. // encode literals length
  224. if (literals_length >= runMask) {
  225. // add match length to the token
  226. dst[dpos++] = (runMask << mlBits) + token
  227. for (var len = literals_length - runMask; len > 254; len -= 255) {
  228. dst[dpos++] = 255
  229. }
  230. dst[dpos++] = len
  231. } else {
  232. // add match length to the token
  233. dst[dpos++] = (literals_length << mlBits) + token
  234. }
  235.  
  236. // write literals
  237. for (var i = 0; i < literals_length; i++) {
  238. dst[dpos++] = src[anchor+i]
  239. }
  240.  
  241. // encode offset
  242. dst[dpos++] = offset
  243. dst[dpos++] = (offset >> 8)
  244.  
  245. // encode match length
  246. if (match_length >= mlMask) {
  247. match_length -= mlMask
  248. while (match_length >= 255) {
  249. match_length -= 255
  250. dst[dpos++] = 255
  251. }
  252.  
  253. dst[dpos++] = match_length
  254. }
  255.  
  256. anchor = pos
  257. }
  258. }
  259.  
  260. // cannot compress input
  261. if (anchor == 0) return 0
  262.  
  263. // Write last literals
  264. // encode literals length
  265. literals_length = src.length - anchor
  266. if (literals_length >= runMask) {
  267. // add match length to the token
  268. dst[dpos++] = (runMask << mlBits)
  269. for (var ln = literals_length - runMask; ln > 254; ln -= 255) {
  270. dst[dpos++] = 255
  271. }
  272. dst[dpos++] = ln
  273. } else {
  274. // add match length to the token
  275. dst[dpos++] = (literals_length << mlBits)
  276. }
  277.  
  278. // write literals
  279. pos = anchor
  280. while (pos < src.length) {
  281. dst[dpos++] = src[pos++]
  282. }
  283.  
  284. return dpos
  285. }
  286.  
  287. },{"cuint":7}],2:[function(require,module,exports){
  288. (function (Buffer){
  289. var Decoder = require('./decoder_stream')
  290.  
  291. /**
  292. Decode an LZ4 stream
  293. */
  294. function LZ4_uncompress (input, options) {
  295. var output = []
  296. var decoder = new Decoder(options)
  297.  
  298. decoder.on('data', function (chunk) {
  299. output.push(chunk)
  300. })
  301.  
  302. decoder.end(input)
  303.  
  304. return Buffer.concat(output)
  305. }
  306.  
  307. exports.LZ4_uncompress = LZ4_uncompress
  308. }).call(this,require("buffer").Buffer)
  309. },{"./decoder_stream":3,"buffer":"buffer"}],3:[function(require,module,exports){
  310. (function (Buffer){
  311. var Transform = require('stream').Transform
  312. var inherits = require('util').inherits
  313.  
  314. var lz4_static = require('./static')
  315. var utils = lz4_static.utils
  316. var lz4_binding = utils.bindings
  317. var lz4_jsbinding = require('./binding')
  318.  
  319. var STATES = lz4_static.STATES
  320. var SIZES = lz4_static.SIZES
  321.  
  322. function Decoder (options) {
  323. if ( !(this instanceof Decoder) )
  324. return new Decoder(options)
  325. Transform.call(this, options)
  326. // Options
  327. this.options = options || {}
  328.  
  329. this.binding = this.options.useJS ? lz4_jsbinding : lz4_binding
  330.  
  331. // Encoded data being processed
  332. this.buffer = null
  333. // Current position within the data
  334. this.pos = 0
  335. this.descriptor = null
  336.  
  337. // Current state of the parsing
  338. this.state = STATES.MAGIC
  339.  
  340. this.notEnoughData = false
  341. this.descriptorStart = 0
  342. this.streamSize = null
  343. this.dictId = null
  344. this.currentStreamChecksum = null
  345. this.dataBlockSize = 0
  346. this.skippableSize = 0
  347. }
  348. inherits(Decoder, Transform)
  349.  
  350. Decoder.prototype._transform = function (data, encoding, done) {
  351. // Handle skippable data
  352. if (this.skippableSize > 0) {
  353. this.skippableSize -= data.length
  354. if (this.skippableSize > 0) {
  355. // More to skip
  356. done()
  357. return
  358. }
  359.  
  360. data = data.slice(-this.skippableSize)
  361. this.skippableSize = 0
  362. this.state = STATES.MAGIC
  363. }
  364. // Buffer the incoming data
  365. this.buffer = this.buffer
  366. ? Buffer.concat( [ this.buffer, data ], this.buffer.length + data.length )
  367. : data
  368.  
  369. this._main(done)
  370. }
  371.  
  372. Decoder.prototype.emit_Error = function (msg) {
  373. this.emit( 'error', new Error(msg + ' @' + this.pos) )
  374. }
  375.  
  376. Decoder.prototype.check_Size = function (n) {
  377. var delta = this.buffer.length - this.pos
  378. if (delta <= 0 || delta < n) {
  379. if (this.notEnoughData) this.emit_Error( 'Unexpected end of LZ4 stream' )
  380. return true
  381. }
  382.  
  383. this.pos += n
  384. return false
  385. }
  386.  
  387. Decoder.prototype.read_MagicNumber = function () {
  388. var pos = this.pos
  389. if ( this.check_Size(SIZES.MAGIC) ) return true
  390.  
  391. var magic = utils.readInt32LE(this.buffer, pos)
  392.  
  393. // Skippable chunk
  394. if ( (magic & 0xFFFFFFF0) === lz4_static.MAGICNUMBER_SKIPPABLE ) {
  395. this.state = STATES.SKIP_SIZE
  396. return
  397. }
  398.  
  399. // LZ4 stream
  400. if ( magic !== lz4_static.MAGICNUMBER ) {
  401. this.pos = pos
  402. this.emit_Error( 'Invalid magic number: ' + magic.toString(16).toUpperCase() )
  403. return true
  404. }
  405.  
  406. this.state = STATES.DESCRIPTOR
  407. }
  408.  
  409. Decoder.prototype.read_SkippableSize = function () {
  410. var pos = this.pos
  411. if ( this.check_Size(SIZES.SKIP_SIZE) ) return true
  412. this.state = STATES.SKIP_DATA
  413. this.skippableSize = utils.readInt32LE(this.buffer, pos)
  414. }
  415.  
  416. Decoder.prototype.read_Descriptor = function () {
  417. // Flags
  418. var pos = this.pos
  419. if ( this.check_Size(SIZES.DESCRIPTOR) ) return true
  420.  
  421. this.descriptorStart = pos
  422.  
  423. // version
  424. var descriptor_flg = this.buffer[pos]
  425. var version = descriptor_flg >> 6
  426. if ( version !== lz4_static.VERSION ) {
  427. this.pos = pos
  428. this.emit_Error( 'Invalid version: ' + version + ' != ' + lz4_static.VERSION )
  429. return true
  430. }
  431.  
  432. // flags
  433. // reserved bit should not be set
  434. if ( (descriptor_flg >> 1) & 0x1 ) {
  435. this.pos = pos
  436. this.emit_Error('Reserved bit set')
  437. return true
  438. }
  439.  
  440. var blockMaxSizeIndex = (this.buffer[pos+1] >> 4) & 0x7
  441. var blockMaxSize = lz4_static.blockMaxSizes[ blockMaxSizeIndex ]
  442. if ( blockMaxSize === null ) {
  443. this.pos = pos
  444. this.emit_Error( 'Invalid block max size: ' + blockMaxSizeIndex )
  445. return true
  446. }
  447.  
  448. this.descriptor = {
  449. blockIndependence: Boolean( (descriptor_flg >> 5) & 0x1 )
  450. , blockChecksum: Boolean( (descriptor_flg >> 4) & 0x1 )
  451. , blockMaxSize: blockMaxSize
  452. , streamSize: Boolean( (descriptor_flg >> 3) & 0x1 )
  453. , streamChecksum: Boolean( (descriptor_flg >> 2) & 0x1 )
  454. , dict: Boolean( descriptor_flg & 0x1 )
  455. , dictId: 0
  456. }
  457.  
  458. this.state = STATES.SIZE
  459. }
  460.  
  461. Decoder.prototype.read_Size = function () {
  462. if (this.descriptor.streamSize) {
  463. var pos = this.pos
  464. if ( this.check_Size(SIZES.SIZE) ) return true
  465. //TODO max size is unsigned 64 bits
  466. this.streamSize = this.buffer.slice(pos, pos + 8)
  467. }
  468.  
  469. this.state = STATES.DICTID
  470. }
  471.  
  472. Decoder.prototype.read_DictId = function () {
  473. if (this.descriptor.dictId) {
  474. var pos = this.pos
  475. if ( this.check_Size(SIZES.DICTID) ) return true
  476. this.dictId = utils.readInt32LE(this.buffer, pos)
  477. }
  478.  
  479. this.state = STATES.DESCRIPTOR_CHECKSUM
  480. }
  481.  
  482. Decoder.prototype.read_DescriptorChecksum = function () {
  483. var pos = this.pos
  484. if ( this.check_Size(SIZES.DESCRIPTOR_CHECKSUM) ) return true
  485.  
  486. var checksum = this.buffer[pos]
  487. var currentChecksum = utils.descriptorChecksum( this.buffer.slice(this.descriptorStart, pos) )
  488. if (currentChecksum !== checksum) {
  489. this.pos = pos
  490. this.emit_Error( 'Invalid stream descriptor checksum' )
  491. return true
  492. }
  493.  
  494. this.state = STATES.DATABLOCK_SIZE
  495. }
  496.  
  497. Decoder.prototype.read_DataBlockSize = function () {
  498. var pos = this.pos
  499. if ( this.check_Size(SIZES.DATABLOCK_SIZE) ) return true
  500. var datablock_size = utils.readInt32LE(this.buffer, pos)
  501. // Uncompressed
  502. if ( datablock_size === lz4_static.EOS ) {
  503. this.state = STATES.EOS
  504. return
  505. }
  506.  
  507. // if (datablock_size > this.descriptor.blockMaxSize) {
  508. // this.emit_Error( 'ASSERTION: invalid datablock_size: ' + datablock_size.toString(16).toUpperCase() + ' > ' + this.descriptor.blockMaxSize.toString(16).toUpperCase() )
  509. // }
  510. this.dataBlockSize = datablock_size
  511.  
  512. this.state = STATES.DATABLOCK_DATA
  513. }
  514.  
  515. Decoder.prototype.read_DataBlockData = function () {
  516. var pos = this.pos
  517. var datablock_size = this.dataBlockSize
  518. if ( datablock_size & 0x80000000 ) {
  519. // Uncompressed size
  520. datablock_size = datablock_size & 0x7FFFFFFF
  521. }
  522. if ( this.check_Size(datablock_size) ) return true
  523.  
  524. this.dataBlock = this.buffer.slice(pos, pos + datablock_size)
  525.  
  526. this.state = STATES.DATABLOCK_CHECKSUM
  527. }
  528.  
  529. Decoder.prototype.read_DataBlockChecksum = function () {
  530. var pos = this.pos
  531. if (this.descriptor.blockChecksum) {
  532. if ( this.check_Size(SIZES.DATABLOCK_CHECKSUM) ) return true
  533. var checksum = utils.readInt32LE(this.buffer, this.pos-4)
  534. var currentChecksum = utils.blockChecksum( this.dataBlock )
  535. if (currentChecksum !== checksum) {
  536. this.pos = pos
  537. this.emit_Error( 'Invalid block checksum' )
  538. return true
  539. }
  540. }
  541.  
  542. this.state = STATES.DATABLOCK_UNCOMPRESS
  543. }
  544.  
  545. Decoder.prototype.uncompress_DataBlock = function () {
  546. var uncompressed
  547. // uncompressed?
  548. if ( this.dataBlockSize & 0x80000000 ) {
  549. uncompressed = this.dataBlock
  550. } else {
  551. uncompressed = new Buffer(this.descriptor.blockMaxSize)
  552. var decodedSize = this.binding.uncompress( this.dataBlock, uncompressed )
  553. if (decodedSize < 0) {
  554. this.emit_Error( 'Invalid data block: ' + (-decodedSize) )
  555. return true
  556. }
  557. if ( decodedSize < this.descriptor.blockMaxSize )
  558. uncompressed = uncompressed.slice(0, decodedSize)
  559. }
  560. this.dataBlock = null
  561. this.push( uncompressed )
  562.  
  563. // Stream checksum
  564. if (this.descriptor.streamChecksum) {
  565. this.currentStreamChecksum = utils.streamChecksum(uncompressed, this.currentStreamChecksum)
  566. }
  567.  
  568. this.state = STATES.DATABLOCK_SIZE
  569. }
  570.  
  571. Decoder.prototype.read_EOS = function () {
  572. if (this.descriptor.streamChecksum) {
  573. var pos = this.pos
  574. if ( this.check_Size(SIZES.EOS) ) return true
  575. var checksum = utils.readInt32LE(this.buffer, pos)
  576. if ( checksum !== utils.streamChecksum(null, this.currentStreamChecksum) ) {
  577. this.pos = pos
  578. this.emit_Error( 'Invalid stream checksum: ' + checksum.toString(16).toUpperCase() )
  579. return true
  580. }
  581. }
  582.  
  583. this.state = STATES.MAGIC
  584. }
  585.  
  586. Decoder.prototype._flush = function (done) {
  587. // Error on missing data as no more will be coming
  588. this.notEnoughData = true
  589. this._main(done)
  590. }
  591.  
  592. Decoder.prototype._main = function (done) {
  593. var pos = this.pos
  594. var notEnoughData
  595.  
  596. while ( !notEnoughData && this.pos < this.buffer.length ) {
  597. if (this.state === STATES.MAGIC)
  598. notEnoughData = this.read_MagicNumber()
  599.  
  600. if (this.state === STATES.SKIP_SIZE)
  601. notEnoughData = this.read_SkippableSize()
  602.  
  603. if (this.state === STATES.DESCRIPTOR)
  604. notEnoughData = this.read_Descriptor()
  605.  
  606. if (this.state === STATES.SIZE)
  607. notEnoughData = this.read_Size()
  608.  
  609. if (this.state === STATES.DICTID)
  610. notEnoughData = this.read_DictId()
  611.  
  612. if (this.state === STATES.DESCRIPTOR_CHECKSUM)
  613. notEnoughData = this.read_DescriptorChecksum()
  614.  
  615. if (this.state === STATES.DATABLOCK_SIZE)
  616. notEnoughData = this.read_DataBlockSize()
  617.  
  618. if (this.state === STATES.DATABLOCK_DATA)
  619. notEnoughData = this.read_DataBlockData()
  620.  
  621. if (this.state === STATES.DATABLOCK_CHECKSUM)
  622. notEnoughData = this.read_DataBlockChecksum()
  623.  
  624. if (this.state === STATES.DATABLOCK_UNCOMPRESS)
  625. notEnoughData = this.uncompress_DataBlock()
  626.  
  627. if (this.state === STATES.EOS)
  628. notEnoughData = this.read_EOS()
  629. }
  630.  
  631. if (this.pos > pos) {
  632. this.buffer = this.buffer.slice(this.pos)
  633. this.pos = 0
  634. }
  635.  
  636. done()
  637. }
  638.  
  639. module.exports = Decoder
  640.  
  641. }).call(this,require("buffer").Buffer)
  642. },{"./binding":1,"./static":6,"buffer":"buffer","stream":33,"util":36}],4:[function(require,module,exports){
  643. (function (Buffer){
  644. var Encoder = require('./encoder_stream')
  645.  
  646. /**
  647. Encode an LZ4 stream
  648. */
  649. function LZ4_compress (input, options) {
  650. var output = []
  651. var encoder = new Encoder(options)
  652.  
  653. encoder.on('data', function (chunk) {
  654. output.push(chunk)
  655. })
  656.  
  657. encoder.end(input)
  658.  
  659. return Buffer.concat(output)
  660. }
  661.  
  662. exports.LZ4_compress = LZ4_compress
  663.  
  664. }).call(this,require("buffer").Buffer)
  665. },{"./encoder_stream":5,"buffer":"buffer"}],5:[function(require,module,exports){
  666. (function (Buffer){
  667. var Transform = require('stream').Transform
  668. var inherits = require('util').inherits
  669.  
  670. var lz4_static = require('./static')
  671. var utils = lz4_static.utils
  672. var lz4_binding = utils.bindings
  673. var lz4_jsbinding = require('./binding')
  674.  
  675. var STATES = lz4_static.STATES
  676. var SIZES = lz4_static.SIZES
  677.  
  678. var defaultOptions = {
  679. blockIndependence: true
  680. , blockChecksum: false
  681. , blockMaxSize: 4<<20
  682. , streamSize: false
  683. , streamChecksum: true
  684. , dict: false
  685. , dictId: 0
  686. , highCompression: false
  687. }
  688.  
  689. function Encoder (options) {
  690. if ( !(this instanceof Encoder) )
  691. return new Encoder(options)
  692. Transform.call(this, options)
  693.  
  694. // Set the options
  695. var o = options || defaultOptions
  696. if (o !== defaultOptions)
  697. Object.keys(defaultOptions).forEach(function (p) {
  698. if ( !o.hasOwnProperty(p) ) o[p] = defaultOptions[p]
  699. })
  700.  
  701. this.options = o
  702.  
  703. this.binding = this.options.useJS ? lz4_jsbinding : lz4_binding
  704. this.compress = o.highCompression ? this.binding.compressHC : this.binding.compress
  705.  
  706. // Build the stream descriptor from the options
  707. // flags
  708. var descriptor_flg = 0
  709. descriptor_flg = descriptor_flg | (lz4_static.VERSION << 6) // Version
  710. descriptor_flg = descriptor_flg | ((o.blockIndependence & 1) << 5) // Block independence
  711. descriptor_flg = descriptor_flg | ((o.blockChecksum & 1) << 4) // Block checksum
  712. descriptor_flg = descriptor_flg | ((o.streamSize & 1) << 3) // Stream size
  713. descriptor_flg = descriptor_flg | ((o.streamChecksum & 1) << 2) // Stream checksum
  714. // Reserved bit
  715. descriptor_flg = descriptor_flg | (o.dict & 1) // Preset dictionary
  716.  
  717. // block maximum size
  718. var descriptor_bd = lz4_static.blockMaxSizes.indexOf(o.blockMaxSize)
  719. if (descriptor_bd < 0)
  720. throw new Error('Invalid blockMaxSize: ' + o.blockMaxSize)
  721.  
  722. this.descriptor = { flg: descriptor_flg, bd: (descriptor_bd & 0x7) << 4 }
  723.  
  724. // Data being processed
  725. this.buffer = []
  726. this.length = 0
  727.  
  728. this.first = true
  729. this.checksum = null
  730. }
  731. inherits(Encoder, Transform)
  732.  
  733. // Header = magic number + stream descriptor
  734. Encoder.prototype.headerSize = function () {
  735. var streamSizeSize = this.options.streamSize ? SIZES.DESCRIPTOR : 0
  736. var dictSize = this.options.dict ? SIZES.DICTID : 0
  737.  
  738. return SIZES.MAGIC + 1 + 1 + streamSizeSize + dictSize + 1
  739. }
  740.  
  741. Encoder.prototype.header = function () {
  742. var headerSize = this.headerSize()
  743. var output = new Buffer(headerSize)
  744.  
  745. this.state = STATES.MAGIC
  746. output.writeInt32LE(lz4_static.MAGICNUMBER, 0, true)
  747.  
  748. this.state = STATES.DESCRIPTOR
  749. var descriptor = output.slice(SIZES.MAGIC, output.length - 1)
  750.  
  751. // Update the stream descriptor
  752. descriptor.writeUInt8(this.descriptor.flg, 0, true)
  753. descriptor.writeUInt8(this.descriptor.bd, 1, true)
  754.  
  755. var pos = 2
  756. this.state = STATES.SIZE
  757. if (this.options.streamSize) {
  758. //TODO only 32bits size supported
  759. descriptor.writeInt32LE(0, pos, true)
  760. descriptor.writeInt32LE(this.size, pos + 4, true)
  761. pos += SIZES.SIZE
  762. }
  763. this.state = STATES.DICTID
  764. if (this.options.dict) {
  765. descriptor.writeInt32LE(this.dictId, pos, true)
  766. pos += SIZES.DICTID
  767. }
  768.  
  769. this.state = STATES.DESCRIPTOR_CHECKSUM
  770. output.writeUInt8(
  771. utils.descriptorChecksum( descriptor )
  772. , SIZES.MAGIC + pos, false
  773. )
  774.  
  775. return output
  776. }
  777.  
  778. Encoder.prototype.update_Checksum = function (data) {
  779. // Calculate the stream checksum
  780. this.state = STATES.CHECKSUM_UPDATE
  781. if (this.options.streamChecksum) {
  782. this.checksum = utils.streamChecksum(data, this.checksum)
  783. }
  784. }
  785.  
  786. Encoder.prototype.compress_DataBlock = function (data) {
  787. this.state = STATES.DATABLOCK_COMPRESS
  788. var dbChecksumSize = this.options.blockChecksum ? SIZES.DATABLOCK_CHECKSUM : 0
  789. var maxBufSize = this.binding.compressBound(data.length)
  790. var buf = new Buffer( SIZES.DATABLOCK_SIZE + maxBufSize + dbChecksumSize )
  791. var compressed = buf.slice(SIZES.DATABLOCK_SIZE, SIZES.DATABLOCK_SIZE + maxBufSize)
  792. var compressedSize = this.compress(data, compressed)
  793.  
  794. // Set the block size
  795. this.state = STATES.DATABLOCK_SIZE
  796. // Block size shall never be larger than blockMaxSize
  797. // console.log("blockMaxSize", this.options.blockMaxSize, "compressedSize", compressedSize)
  798. if (compressedSize > 0 && compressedSize <= this.options.blockMaxSize) {
  799. // highest bit is 0 (compressed data)
  800. buf.writeUInt32LE(compressedSize, 0, true)
  801. buf = buf.slice(0, SIZES.DATABLOCK_SIZE + compressedSize + dbChecksumSize)
  802. } else {
  803. // Cannot compress the data, leave it as is
  804. // highest bit is 1 (uncompressed data)
  805. buf.writeInt32LE( 0x80000000 | data.length, 0, true)
  806. buf = buf.slice(0, SIZES.DATABLOCK_SIZE + data.length + dbChecksumSize)
  807. data.copy(buf, SIZES.DATABLOCK_SIZE);
  808. }
  809.  
  810. // Set the block checksum
  811. this.state = STATES.DATABLOCK_CHECKSUM
  812. if (this.options.blockChecksum) {
  813. // xxHash checksum on undecoded data with a seed of 0
  814. var checksum = buf.slice(-dbChecksumSize)
  815. checksum.writeInt32LE( utils.blockChecksum(compressed), 0, true )
  816. }
  817.  
  818. // Update the stream checksum
  819. this.update_Checksum(data)
  820.  
  821. this.size += data.length
  822.  
  823. return buf
  824. }
  825.  
  826. Encoder.prototype._transform = function (data, encoding, done) {
  827. if (data) {
  828. // Buffer the incoming data
  829. this.buffer.push(data)
  830. this.length += data.length
  831. }
  832.  
  833. // Stream header
  834. if (this.first) {
  835. this.push( this.header() )
  836. this.first = false
  837. }
  838.  
  839. var blockMaxSize = this.options.blockMaxSize
  840. // Not enough data for a block
  841. if ( this.length < blockMaxSize ) return done()
  842.  
  843. // Build the data to be compressed
  844. var buf = Buffer.concat(this.buffer, this.length)
  845.  
  846. for (var j = 0, i = buf.length; i >= blockMaxSize; i -= blockMaxSize, j += blockMaxSize) {
  847. // Compress the block
  848. this.push( this.compress_DataBlock( buf.slice(j, j + blockMaxSize) ) )
  849. }
  850.  
  851. // Set the remaining data
  852. if (i > 0) {
  853. this.buffer = [ buf.slice(j) ]
  854. this.length = this.buffer[0].length
  855. } else {
  856. this.buffer = []
  857. this.length = 0
  858. }
  859.  
  860. done()
  861. }
  862.  
  863. Encoder.prototype._flush = function (done) {
  864. if (this.length > 0) {
  865. var buf = Buffer.concat(this.buffer, this.length)
  866. this.buffer = []
  867. this.length = 0
  868. var cc = this.compress_DataBlock(buf)
  869. this.push( cc )
  870. }
  871.  
  872. if (this.options.streamChecksum) {
  873. this.state = STATES.CHECKSUM
  874. var eos = new Buffer(SIZES.EOS + SIZES.CHECKSUM)
  875. eos.writeInt32LE( utils.streamChecksum(null, this.checksum), SIZES.EOS, true )
  876. } else {
  877. var eos = new Buffer(SIZES.EOS)
  878. }
  879.  
  880. this.state = STATES.EOS
  881. eos.writeInt32LE(lz4_static.EOS, 0, true)
  882. this.push(eos)
  883.  
  884. done()
  885. }
  886.  
  887. module.exports = Encoder
  888.  
  889. }).call(this,require("buffer").Buffer)
  890. },{"./binding":1,"./static":6,"buffer":"buffer","stream":33,"util":36}],6:[function(require,module,exports){
  891. (function (Buffer){
  892. /**
  893. * LZ4 based compression and decompression
  894. * Copyright (c) 2014 Pierre Curto
  895. * MIT Licensed
  896. */
  897.  
  898. // LZ4 stream constants
  899. exports.MAGICNUMBER = 0x184D2204
  900. exports.MAGICNUMBER_BUFFER = new Buffer(4)
  901. exports.MAGICNUMBER_BUFFER.writeUInt32LE(exports.MAGICNUMBER, 0, false)
  902.  
  903. exports.EOS = 0
  904. exports.EOS_BUFFER = new Buffer(4)
  905. exports.EOS_BUFFER.writeUInt32LE(exports.EOS, 0, false)
  906.  
  907. exports.VERSION = 1
  908.  
  909. exports.MAGICNUMBER_SKIPPABLE = 0x184D2A50
  910.  
  911. // n/a, n/a, n/a, n/a, 64KB, 256KB, 1MB, 4MB
  912. exports.blockMaxSizes = [ null, null, null, null, 64<<10, 256<<10, 1<<20, 4<<20 ]
  913.  
  914. // Compressed file extension
  915. exports.extension = '.lz4'
  916.  
  917. // Internal stream states
  918. exports.STATES = {
  919. // Compressed stream
  920. MAGIC: 0
  921. , DESCRIPTOR: 1
  922. , SIZE: 2
  923. , DICTID: 3
  924. , DESCRIPTOR_CHECKSUM: 4
  925. , DATABLOCK_SIZE: 5
  926. , DATABLOCK_DATA: 6
  927. , DATABLOCK_CHECKSUM: 7
  928. , DATABLOCK_UNCOMPRESS: 8
  929. , DATABLOCK_COMPRESS: 9
  930. , CHECKSUM: 10
  931. , CHECKSUM_UPDATE: 11
  932. , EOS: 90
  933. // Skippable chunk
  934. , SKIP_SIZE: 101
  935. , SKIP_DATA: 102
  936. }
  937.  
  938. exports.SIZES = {
  939. MAGIC: 4
  940. , DESCRIPTOR: 2
  941. , SIZE: 8
  942. , DICTID: 4
  943. , DESCRIPTOR_CHECKSUM: 1
  944. , DATABLOCK_SIZE: 4
  945. , DATABLOCK_CHECKSUM: 4
  946. , CHECKSUM: 4
  947. , EOS: 4
  948. , SKIP_SIZE: 4
  949. }
  950.  
  951. exports.utils = require('./utils')
  952.  
  953. }).call(this,require("buffer").Buffer)
  954. },{"./utils":"./utils","buffer":"buffer"}],7:[function(require,module,exports){
  955. exports.UINT32 = require('./lib/uint32')
  956. exports.UINT64 = require('./lib/uint64')
  957. },{"./lib/uint32":8,"./lib/uint64":9}],8:[function(require,module,exports){
  958. /**
  959. C-like unsigned 32 bits integers in Javascript
  960. Copyright (C) 2013, Pierre Curto
  961. MIT license
  962. */
  963. ;(function (root) {
  964.  
  965. // Local cache for typical radices
  966. var radixPowerCache = {
  967. 36: UINT32( Math.pow(36, 5) )
  968. , 16: UINT32( Math.pow(16, 7) )
  969. , 10: UINT32( Math.pow(10, 9) )
  970. , 2: UINT32( Math.pow(2, 30) )
  971. }
  972. var radixCache = {
  973. 36: UINT32(36)
  974. , 16: UINT32(16)
  975. , 10: UINT32(10)
  976. , 2: UINT32(2)
  977. }
  978.  
  979. /**
  980. * Represents an unsigned 32 bits integer
  981. * @constructor
  982. * @param {Number|String|Number} low bits | integer as a string | integer as a number
  983. * @param {Number|Number|Undefined} high bits | radix (optional, default=10)
  984. * @return
  985. */
  986. function UINT32 (l, h) {
  987. if ( !(this instanceof UINT32) )
  988. return new UINT32(l, h)
  989.  
  990. this._low = 0
  991. this._high = 0
  992. this.remainder = null
  993. if (typeof h == 'undefined')
  994. return fromNumber.call(this, l)
  995.  
  996. if (typeof l == 'string')
  997. return fromString.call(this, l, h)
  998.  
  999. fromBits.call(this, l, h)
  1000. }
  1001.  
  1002. /**
  1003. * Set the current _UINT32_ object with its low and high bits
  1004. * @method fromBits
  1005. * @param {Number} low bits
  1006. * @param {Number} high bits
  1007. * @return ThisExpression
  1008. */
  1009. function fromBits (l, h) {
  1010. this._low = l | 0
  1011. this._high = h | 0
  1012.  
  1013. return this
  1014. }
  1015. UINT32.prototype.fromBits = fromBits
  1016.  
  1017. /**
  1018. * Set the current _UINT32_ object from a number
  1019. * @method fromNumber
  1020. * @param {Number} number
  1021. * @return ThisExpression
  1022. */
  1023. function fromNumber (value) {
  1024. this._low = value & 0xFFFF
  1025. this._high = value >>> 16
  1026.  
  1027. return this
  1028. }
  1029. UINT32.prototype.fromNumber = fromNumber
  1030.  
  1031. /**
  1032. * Set the current _UINT32_ object from a string
  1033. * @method fromString
  1034. * @param {String} integer as a string
  1035. * @param {Number} radix (optional, default=10)
  1036. * @return ThisExpression
  1037. */
  1038. function fromString (s, radix) {
  1039. var value = parseInt(s, radix || 10)
  1040.  
  1041. this._low = value & 0xFFFF
  1042. this._high = value >>> 16
  1043.  
  1044. return this
  1045. }
  1046. UINT32.prototype.fromString = fromString
  1047.  
  1048. /**
  1049. * Convert this _UINT32_ to a number
  1050. * @method toNumber
  1051. * @return {Number} the converted UINT32
  1052. */
  1053. UINT32.prototype.toNumber = function () {
  1054. return (this._high << 16) | this._low
  1055. }
  1056.  
  1057. /**
  1058. * Convert this _UINT32_ to a string
  1059. * @method toString
  1060. * @param {Number} radix (optional, default=10)
  1061. * @return {String} the converted UINT32
  1062. */
  1063. UINT32.prototype.toString = function (radix) {
  1064. radix = radix || 10
  1065. var radixUint = radixCache[radix] || new UINT32(radix)
  1066.  
  1067. if ( !this.gt(radixUint) ) return this.toNumber().toString(radix)
  1068.  
  1069. var self = this.clone()
  1070. var res = new Array(32)
  1071. for (var i = 31; i >= 0; i--) {
  1072. self.div(radixUint)
  1073. res[i] = self.remainder.toNumber().toString(radix)
  1074. if ( !self.gt(radixUint) ) break
  1075. }
  1076. res[i-1] = self.toNumber().toString(radix)
  1077.  
  1078. return res.join('')
  1079. }
  1080.  
  1081. /**
  1082. * Add two _UINT32_. The current _UINT32_ stores the result
  1083. * @method add
  1084. * @param {Object} other UINT32
  1085. * @return ThisExpression
  1086. */
  1087. UINT32.prototype.add = function (other) {
  1088. var a00 = this._low + other._low
  1089. var a16 = a00 >>> 16
  1090.  
  1091. a16 += this._high + other._high
  1092.  
  1093. this._low = a00 & 0xFFFF
  1094. this._high = a16 & 0xFFFF
  1095.  
  1096. return this
  1097. }
  1098.  
  1099. /**
  1100. * Subtract two _UINT32_. The current _UINT32_ stores the result
  1101. * @method subtract
  1102. * @param {Object} other UINT32
  1103. * @return ThisExpression
  1104. */
  1105. UINT32.prototype.subtract = function (other) {
  1106. //TODO inline
  1107. return this.add( other.clone().negate() )
  1108. }
  1109.  
  1110. /**
  1111. * Multiply two _UINT32_. The current _UINT32_ stores the result
  1112. * @method multiply
  1113. * @param {Object} other UINT32
  1114. * @return ThisExpression
  1115. */
  1116. UINT32.prototype.multiply = function (other) {
  1117. /*
  1118. a = a00 + a16
  1119. b = b00 + b16
  1120. a*b = (a00 + a16)(b00 + b16)
  1121. = a00b00 + a00b16 + a16b00 + a16b16
  1122.  
  1123. a16b16 overflows the 32bits
  1124. */
  1125. var a16 = this._high
  1126. var a00 = this._low
  1127. var b16 = other._high
  1128. var b00 = other._low
  1129.  
  1130. /* Removed to increase speed under normal circumstances (i.e. not multiplying by 0 or 1)
  1131. // this == 0 or other == 1: nothing to do
  1132. if ((a00 == 0 && a16 == 0) || (b00 == 1 && b16 == 0)) return this
  1133.  
  1134. // other == 0 or this == 1: this = other
  1135. if ((b00 == 0 && b16 == 0) || (a00 == 1 && a16 == 0)) {
  1136. this._low = other._low
  1137. this._high = other._high
  1138. return this
  1139. }
  1140. */
  1141.  
  1142. var c16, c00
  1143. c00 = a00 * b00
  1144. c16 = c00 >>> 16
  1145.  
  1146. c16 += a16 * b00
  1147. c16 &= 0xFFFF // Not required but improves performance
  1148. c16 += a00 * b16
  1149.  
  1150. this._low = c00 & 0xFFFF
  1151. this._high = c16 & 0xFFFF
  1152.  
  1153. return this
  1154. }
  1155.  
  1156. /**
  1157. * Divide two _UINT32_. The current _UINT32_ stores the result.
  1158. * The remainder is made available as the _remainder_ property on
  1159. * the _UINT32_ object. It can be null, meaning there are no remainder.
  1160. * @method div
  1161. * @param {Object} other UINT32
  1162. * @return ThisExpression
  1163. */
  1164. UINT32.prototype.div = function (other) {
  1165. if ( (other._low == 0) && (other._high == 0) ) throw Error('division by zero')
  1166.  
  1167. // other == 1
  1168. if (other._high == 0 && other._low == 1) {
  1169. this.remainder = new UINT32(0)
  1170. return this
  1171. }
  1172.  
  1173. // other > this: 0
  1174. if ( other.gt(this) ) {
  1175. this.remainder = new UINT32(0)
  1176. this._low = 0
  1177. this._high = 0
  1178. return this
  1179. }
  1180. // other == this: 1
  1181. if ( this.eq(other) ) {
  1182. this.remainder = new UINT32(0)
  1183. this._low = 1
  1184. this._high = 0
  1185. return this
  1186. }
  1187.  
  1188. // Shift the divisor left until it is higher than the dividend
  1189. var _other = other.clone()
  1190. var i = -1
  1191. while ( !this.lt(_other) ) {
  1192. // High bit can overflow the default 16bits
  1193. // Its ok since we right shift after this loop
  1194. // The overflown bit must be kept though
  1195. _other.shiftLeft(1, true)
  1196. i++
  1197. }
  1198.  
  1199. // Set the remainder
  1200. this.remainder = this.clone()
  1201. // Initialize the current result to 0
  1202. this._low = 0
  1203. this._high = 0
  1204. for (; i >= 0; i--) {
  1205. _other.shiftRight(1)
  1206. // If shifted divisor is smaller than the dividend
  1207. // then subtract it from the dividend
  1208. if ( !this.remainder.lt(_other) ) {
  1209. this.remainder.subtract(_other)
  1210. // Update the current result
  1211. if (i >= 16) {
  1212. this._high |= 1 << (i - 16)
  1213. } else {
  1214. this._low |= 1 << i
  1215. }
  1216. }
  1217. }
  1218.  
  1219. return this
  1220. }
  1221.  
  1222. /**
  1223. * Negate the current _UINT32_
  1224. * @method negate
  1225. * @return ThisExpression
  1226. */
  1227. UINT32.prototype.negate = function () {
  1228. var v = ( ~this._low & 0xFFFF ) + 1
  1229. this._low = v & 0xFFFF
  1230. this._high = (~this._high + (v >>> 16)) & 0xFFFF
  1231.  
  1232. return this
  1233. }
  1234.  
  1235. /**
  1236. * Equals
  1237. * @method eq
  1238. * @param {Object} other UINT32
  1239. * @return {Boolean}
  1240. */
  1241. UINT32.prototype.equals = UINT32.prototype.eq = function (other) {
  1242. return (this._low == other._low) && (this._high == other._high)
  1243. }
  1244.  
  1245. /**
  1246. * Greater than (strict)
  1247. * @method gt
  1248. * @param {Object} other UINT32
  1249. * @return {Boolean}
  1250. */
  1251. UINT32.prototype.greaterThan = UINT32.prototype.gt = function (other) {
  1252. if (this._high > other._high) return true
  1253. if (this._high < other._high) return false
  1254. return this._low > other._low
  1255. }
  1256.  
  1257. /**
  1258. * Less than (strict)
  1259. * @method lt
  1260. * @param {Object} other UINT32
  1261. * @return {Boolean}
  1262. */
  1263. UINT32.prototype.lessThan = UINT32.prototype.lt = function (other) {
  1264. if (this._high < other._high) return true
  1265. if (this._high > other._high) return false
  1266. return this._low < other._low
  1267. }
  1268.  
  1269. /**
  1270. * Bitwise OR
  1271. * @method or
  1272. * @param {Object} other UINT32
  1273. * @return ThisExpression
  1274. */
  1275. UINT32.prototype.or = function (other) {
  1276. this._low |= other._low
  1277. this._high |= other._high
  1278.  
  1279. return this
  1280. }
  1281.  
  1282. /**
  1283. * Bitwise AND
  1284. * @method and
  1285. * @param {Object} other UINT32
  1286. * @return ThisExpression
  1287. */
  1288. UINT32.prototype.and = function (other) {
  1289. this._low &= other._low
  1290. this._high &= other._high
  1291.  
  1292. return this
  1293. }
  1294.  
  1295. /**
  1296. * Bitwise NOT
  1297. * @method not
  1298. * @return ThisExpression
  1299. */
  1300. UINT32.prototype.not = function() {
  1301. this._low = ~this._low & 0xFFFF
  1302. this._high = ~this._high & 0xFFFF
  1303.  
  1304. return this
  1305. }
  1306.  
  1307. /**
  1308. * Bitwise XOR
  1309. * @method xor
  1310. * @param {Object} other UINT32
  1311. * @return ThisExpression
  1312. */
  1313. UINT32.prototype.xor = function (other) {
  1314. this._low ^= other._low
  1315. this._high ^= other._high
  1316.  
  1317. return this
  1318. }
  1319.  
  1320. /**
  1321. * Bitwise shift right
  1322. * @method shiftRight
  1323. * @param {Number} number of bits to shift
  1324. * @return ThisExpression
  1325. */
  1326. UINT32.prototype.shiftRight = UINT32.prototype.shiftr = function (n) {
  1327. if (n > 16) {
  1328. this._low = this._high >> (n - 16)
  1329. this._high = 0
  1330. } else if (n == 16) {
  1331. this._low = this._high
  1332. this._high = 0
  1333. } else {
  1334. this._low = (this._low >> n) | ( (this._high << (16-n)) & 0xFFFF )
  1335. this._high >>= n
  1336. }
  1337.  
  1338. return this
  1339. }
  1340.  
  1341. /**
  1342. * Bitwise shift left
  1343. * @method shiftLeft
  1344. * @param {Number} number of bits to shift
  1345. * @param {Boolean} allow overflow
  1346. * @return ThisExpression
  1347. */
  1348. UINT32.prototype.shiftLeft = UINT32.prototype.shiftl = function (n, allowOverflow) {
  1349. if (n > 16) {
  1350. this._high = this._low << (n - 16)
  1351. this._low = 0
  1352. if (!allowOverflow) {
  1353. this._high &= 0xFFFF
  1354. }
  1355. } else if (n == 16) {
  1356. this._high = this._low
  1357. this._low = 0
  1358. } else {
  1359. this._high = (this._high << n) | (this._low >> (16-n))
  1360. this._low = (this._low << n) & 0xFFFF
  1361. if (!allowOverflow) {
  1362. // Overflow only allowed on the high bits...
  1363. this._high &= 0xFFFF
  1364. }
  1365. }
  1366.  
  1367. return this
  1368. }
  1369.  
  1370. /**
  1371. * Bitwise rotate left
  1372. * @method rotl
  1373. * @param {Number} number of bits to rotate
  1374. * @return ThisExpression
  1375. */
  1376. UINT32.prototype.rotateLeft = UINT32.prototype.rotl = function (n) {
  1377. var v = (this._high << 16) | this._low
  1378. v = (v << n) | (v >>> (32 - n))
  1379. this._low = v & 0xFFFF
  1380. this._high = v >>> 16
  1381.  
  1382. return this
  1383. }
  1384.  
  1385. /**
  1386. * Bitwise rotate right
  1387. * @method rotr
  1388. * @param {Number} number of bits to rotate
  1389. * @return ThisExpression
  1390. */
  1391. UINT32.prototype.rotateRight = UINT32.prototype.rotr = function (n) {
  1392. var v = (this._high << 16) | this._low
  1393. v = (v >>> n) | (v << (32 - n))
  1394. this._low = v & 0xFFFF
  1395. this._high = v >>> 16
  1396.  
  1397. return this
  1398. }
  1399.  
  1400. /**
  1401. * Clone the current _UINT32_
  1402. * @method clone
  1403. * @return {Object} cloned UINT32
  1404. */
  1405. UINT32.prototype.clone = function () {
  1406. return new UINT32(this._low, this._high)
  1407. }
  1408.  
  1409. if (typeof define != 'undefined' && define.amd) {
  1410. // AMD / RequireJS
  1411. define([], function () {
  1412. return UINT32
  1413. })
  1414. } else if (typeof module != 'undefined' && module.exports) {
  1415. // Node.js
  1416. module.exports = UINT32
  1417. } else {
  1418. // Browser
  1419. root['UINT32'] = UINT32
  1420. }
  1421.  
  1422. })(this)
  1423.  
  1424. },{}],9:[function(require,module,exports){
  1425. /**
  1426. C-like unsigned 64 bits integers in Javascript
  1427. Copyright (C) 2013, Pierre Curto
  1428. MIT license
  1429. */
  1430. ;(function (root) {
  1431.  
  1432. // Local cache for typical radices
  1433. var radixPowerCache = {
  1434. 16: UINT64( Math.pow(16, 5) )
  1435. , 10: UINT64( Math.pow(10, 5) )
  1436. , 2: UINT64( Math.pow(2, 5) )
  1437. }
  1438. var radixCache = {
  1439. 16: UINT64(16)
  1440. , 10: UINT64(10)
  1441. , 2: UINT64(2)
  1442. }
  1443.  
  1444. /**
  1445. * Represents an unsigned 64 bits integer
  1446. * @constructor
  1447. * @param {Number} first low bits (8)
  1448. * @param {Number} second low bits (8)
  1449. * @param {Number} first high bits (8)
  1450. * @param {Number} second high bits (8)
  1451. * or
  1452. * @param {Number} low bits (32)
  1453. * @param {Number} high bits (32)
  1454. * or
  1455. * @param {String|Number} integer as a string | integer as a number
  1456. * @param {Number|Undefined} radix (optional, default=10)
  1457. * @return
  1458. */
  1459. function UINT64 (a00, a16, a32, a48) {
  1460. if ( !(this instanceof UINT64) )
  1461. return new UINT64(a00, a16, a32, a48)
  1462.  
  1463. this.remainder = null
  1464. if (typeof a00 == 'string')
  1465. return fromString.call(this, a00, a16)
  1466.  
  1467. if (typeof a16 == 'undefined')
  1468. return fromNumber.call(this, a00)
  1469.  
  1470. fromBits.apply(this, arguments)
  1471. }
  1472.  
  1473. /**
  1474. * Set the current _UINT64_ object with its low and high bits
  1475. * @method fromBits
  1476. * @param {Number} first low bits (8)
  1477. * @param {Number} second low bits (8)
  1478. * @param {Number} first high bits (8)
  1479. * @param {Number} second high bits (8)
  1480. * or
  1481. * @param {Number} low bits (32)
  1482. * @param {Number} high bits (32)
  1483. * @return ThisExpression
  1484. */
  1485. function fromBits (a00, a16, a32, a48) {
  1486. if (typeof a32 == 'undefined') {
  1487. this._a00 = a00 & 0xFFFF
  1488. this._a16 = a00 >>> 16
  1489. this._a32 = a16 & 0xFFFF
  1490. this._a48 = a16 >>> 16
  1491. return this
  1492. }
  1493.  
  1494. this._a00 = a00 | 0
  1495. this._a16 = a16 | 0
  1496. this._a32 = a32 | 0
  1497. this._a48 = a48 | 0
  1498.  
  1499. return this
  1500. }
  1501. UINT64.prototype.fromBits = fromBits
  1502.  
  1503. /**
  1504. * Set the current _UINT64_ object from a number
  1505. * @method fromNumber
  1506. * @param {Number} number
  1507. * @return ThisExpression
  1508. */
  1509. function fromNumber (value) {
  1510. this._a00 = value & 0xFFFF
  1511. this._a16 = value >>> 16
  1512. this._a32 = 0
  1513. this._a48 = 0
  1514.  
  1515. return this
  1516. }
  1517. UINT64.prototype.fromNumber = fromNumber
  1518.  
  1519. /**
  1520. * Set the current _UINT64_ object from a string
  1521. * @method fromString
  1522. * @param {String} integer as a string
  1523. * @param {Number} radix (optional, default=10)
  1524. * @return ThisExpression
  1525. */
  1526. function fromString (s, radix) {
  1527. radix = radix || 10
  1528.  
  1529. this._a00 = 0
  1530. this._a16 = 0
  1531. this._a32 = 0
  1532. this._a48 = 0
  1533.  
  1534. /*
  1535. In Javascript, bitwise operators only operate on the first 32 bits
  1536. of a number, even though parseInt() encodes numbers with a 53 bits
  1537. mantissa.
  1538. Therefore UINT64(<Number>) can only work on 32 bits.
  1539. The radix maximum value is 36 (as per ECMA specs) (26 letters + 10 digits)
  1540. maximum input value is m = 32bits as 1 = 2^32 - 1
  1541. So the maximum substring length n is:
  1542. 36^(n+1) - 1 = 2^32 - 1
  1543. 36^(n+1) = 2^32
  1544. (n+1)ln(36) = 32ln(2)
  1545. n = 32ln(2)/ln(36) - 1
  1546. n = 5.189644915687692
  1547. n = 5
  1548. */
  1549. var radixUint = radixPowerCache[radix] || new UINT64( Math.pow(radix, 5) )
  1550.  
  1551. for (var i = 0, len = s.length; i < len; i += 5) {
  1552. var size = Math.min(5, len - i)
  1553. var value = parseInt( s.slice(i, i + size), radix )
  1554. this.multiply(
  1555. size < 5
  1556. ? new UINT64( Math.pow(radix, size) )
  1557. : radixUint
  1558. )
  1559. .add( new UINT64(value) )
  1560. }
  1561.  
  1562. return this
  1563. }
  1564. UINT64.prototype.fromString = fromString
  1565.  
  1566. /**
  1567. * Convert this _UINT64_ to a number (last 32 bits are dropped)
  1568. * @method toNumber
  1569. * @return {Number} the converted UINT64
  1570. */
  1571. UINT64.prototype.toNumber = function () {
  1572. return (this._a16 << 16) | this._a00
  1573. }
  1574.  
  1575. /**
  1576. * Convert this _UINT64_ to a string
  1577. * @method toString
  1578. * @param {Number} radix (optional, default=10)
  1579. * @return {String} the converted UINT64
  1580. */
  1581. UINT64.prototype.toString = function (radix) {
  1582. radix = radix || 10
  1583. var radixUint = radixCache[radix] || new UINT64(radix)
  1584.  
  1585. if ( !this.gt(radixUint) ) return this.toNumber().toString(radix)
  1586.  
  1587. var self = this.clone()
  1588. var res = new Array(64)
  1589. for (var i = 63; i >= 0; i--) {
  1590. self.div(radixUint)
  1591. res[i] = self.remainder.toNumber().toString(radix)
  1592. if ( !self.gt(radixUint) ) break
  1593. }
  1594. res[i-1] = self.toNumber().toString(radix)
  1595.  
  1596. return res.join('')
  1597. }
  1598.  
  1599. /**
  1600. * Add two _UINT64_. The current _UINT64_ stores the result
  1601. * @method add
  1602. * @param {Object} other UINT64
  1603. * @return ThisExpression
  1604. */
  1605. UINT64.prototype.add = function (other) {
  1606. var a00 = this._a00 + other._a00
  1607.  
  1608. var a16 = a00 >>> 16
  1609. a16 += this._a16 + other._a16
  1610.  
  1611. var a32 = a16 >>> 16
  1612. a32 += this._a32 + other._a32
  1613.  
  1614. var a48 = a32 >>> 16
  1615. a48 += this._a48 + other._a48
  1616.  
  1617. this._a00 = a00 & 0xFFFF
  1618. this._a16 = a16 & 0xFFFF
  1619. this._a32 = a32 & 0xFFFF
  1620. this._a48 = a48 & 0xFFFF
  1621.  
  1622. return this
  1623. }
  1624.  
  1625. /**
  1626. * Subtract two _UINT64_. The current _UINT64_ stores the result
  1627. * @method subtract
  1628. * @param {Object} other UINT64
  1629. * @return ThisExpression
  1630. */
  1631. UINT64.prototype.subtract = function (other) {
  1632. return this.add( other.clone().negate() )
  1633. }
  1634.  
  1635. /**
  1636. * Multiply two _UINT64_. The current _UINT64_ stores the result
  1637. * @method multiply
  1638. * @param {Object} other UINT64
  1639. * @return ThisExpression
  1640. */
  1641. UINT64.prototype.multiply = function (other) {
  1642. /*
  1643. a = a00 + a16 + a32 + a48
  1644. b = b00 + b16 + b32 + b48
  1645. a*b = (a00 + a16 + a32 + a48)(b00 + b16 + b32 + b48)
  1646. = a00b00 + a00b16 + a00b32 + a00b48
  1647. + a16b00 + a16b16 + a16b32 + a16b48
  1648. + a32b00 + a32b16 + a32b32 + a32b48
  1649. + a48b00 + a48b16 + a48b32 + a48b48
  1650.  
  1651. a16b48, a32b32, a48b16, a48b32 and a48b48 overflow the 64 bits
  1652. so it comes down to:
  1653. a*b = a00b00 + a00b16 + a00b32 + a00b48
  1654. + a16b00 + a16b16 + a16b32
  1655. + a32b00 + a32b16
  1656. + a48b00
  1657. = a00b00
  1658. + a00b16 + a16b00
  1659. + a00b32 + a16b16 + a32b00
  1660. + a00b48 + a16b32 + a32b16 + a48b00
  1661. */
  1662. var a00 = this._a00
  1663. var a16 = this._a16
  1664. var a32 = this._a32
  1665. var a48 = this._a48
  1666. var b00 = other._a00
  1667. var b16 = other._a16
  1668. var b32 = other._a32
  1669. var b48 = other._a48
  1670.  
  1671. var c00 = a00 * b00
  1672.  
  1673. var c16 = c00 >>> 16
  1674. c16 += a00 * b16
  1675. var c32 = c16 >>> 16
  1676. c16 &= 0xFFFF
  1677. c16 += a16 * b00
  1678.  
  1679. c32 += c16 >>> 16
  1680. c32 += a00 * b32
  1681. var c48 = c32 >>> 16
  1682. c32 &= 0xFFFF
  1683. c32 += a16 * b16
  1684. c48 += c32 >>> 16
  1685. c32 &= 0xFFFF
  1686. c32 += a32 * b00
  1687.  
  1688. c48 += c32 >>> 16
  1689. c48 += a00 * b48
  1690. c48 &= 0xFFFF
  1691. c48 += a16 * b32
  1692. c48 &= 0xFFFF
  1693. c48 += a32 * b16
  1694. c48 &= 0xFFFF
  1695. c48 += a48 * b00
  1696.  
  1697. this._a00 = c00 & 0xFFFF
  1698. this._a16 = c16 & 0xFFFF
  1699. this._a32 = c32 & 0xFFFF
  1700. this._a48 = c48 & 0xFFFF
  1701.  
  1702. return this
  1703. }
  1704.  
  1705. /**
  1706. * Divide two _UINT64_. The current _UINT64_ stores the result.
  1707. * The remainder is made available as the _remainder_ property on
  1708. * the _UINT64_ object. It can be null, meaning there are no remainder.
  1709. * @method div
  1710. * @param {Object} other UINT64
  1711. * @return ThisExpression
  1712. */
  1713. UINT64.prototype.div = function (other) {
  1714. if ( (other._a16 == 0) && (other._a32 == 0) && (other._a48 == 0) ) {
  1715. if (other._a00 == 0) throw Error('division by zero')
  1716.  
  1717. // other == 1: this
  1718. if (other._a00 == 1) {
  1719. this.remainder = new UINT64(0)
  1720. return this
  1721. }
  1722. }
  1723.  
  1724. // other > this: 0
  1725. if ( other.gt(this) ) {
  1726. this.remainder = new UINT64(0)
  1727. this._a00 = 0
  1728. this._a16 = 0
  1729. this._a32 = 0
  1730. this._a48 = 0
  1731. return this
  1732. }
  1733. // other == this: 1
  1734. if ( this.eq(other) ) {
  1735. this.remainder = new UINT64(0)
  1736. this._a00 = 1
  1737. this._a16 = 0
  1738. this._a32 = 0
  1739. this._a48 = 0
  1740. return this
  1741. }
  1742.  
  1743. // Shift the divisor left until it is higher than the dividend
  1744. var _other = other.clone()
  1745. var i = -1
  1746. while ( !this.lt(_other) ) {
  1747. // High bit can overflow the default 16bits
  1748. // Its ok since we right shift after this loop
  1749. // The overflown bit must be kept though
  1750. _other.shiftLeft(1, true)
  1751. i++
  1752. }
  1753.  
  1754. // Set the remainder
  1755. this.remainder = this.clone()
  1756. // Initialize the current result to 0
  1757. this._a00 = 0
  1758. this._a16 = 0
  1759. this._a32 = 0
  1760. this._a48 = 0
  1761. for (; i >= 0; i--) {
  1762. _other.shiftRight(1)
  1763. // If shifted divisor is smaller than the dividend
  1764. // then subtract it from the dividend
  1765. if ( !this.remainder.lt(_other) ) {
  1766. this.remainder.subtract(_other)
  1767. // Update the current result
  1768. if (i >= 48) {
  1769. this._a48 |= 1 << (i - 48)
  1770. } else if (i >= 32) {
  1771. this._a32 |= 1 << (i - 32)
  1772. } else if (i >= 16) {
  1773. this._a16 |= 1 << (i - 16)
  1774. } else {
  1775. this._a00 |= 1 << i
  1776. }
  1777. }
  1778. }
  1779.  
  1780. return this
  1781. }
  1782.  
  1783. /**
  1784. * Negate the current _UINT64_
  1785. * @method negate
  1786. * @return ThisExpression
  1787. */
  1788. UINT64.prototype.negate = function () {
  1789. var v = ( ~this._a00 & 0xFFFF ) + 1
  1790. this._a00 = v & 0xFFFF
  1791. v = (~this._a16 & 0xFFFF) + (v >>> 16)
  1792. this._a16 = v & 0xFFFF
  1793. v = (~this._a32 & 0xFFFF) + (v >>> 16)
  1794. this._a32 = v & 0xFFFF
  1795. this._a48 = (~this._a48 + (v >>> 16)) & 0xFFFF
  1796.  
  1797. return this
  1798. }
  1799.  
  1800. /**
  1801.  
  1802. * @method eq
  1803. * @param {Object} other UINT64
  1804. * @return {Boolean}
  1805. */
  1806. UINT64.prototype.equals = UINT64.prototype.eq = function (other) {
  1807. return (this._a48 == other._a48) && (this._a00 == other._a00)
  1808. && (this._a32 == other._a32) && (this._a16 == other._a16)
  1809. }
  1810.  
  1811. /**
  1812. * Greater than (strict)
  1813. * @method gt
  1814. * @param {Object} other UINT64
  1815. * @return {Boolean}
  1816. */
  1817. UINT64.prototype.greaterThan = UINT64.prototype.gt = function (other) {
  1818. if (this._a48 > other._a48) return true
  1819. if (this._a48 < other._a48) return false
  1820. if (this._a32 > other._a32) return true
  1821. if (this._a32 < other._a32) return false
  1822. if (this._a16 > other._a16) return true
  1823. if (this._a16 < other._a16) return false
  1824. return this._a00 > other._a00
  1825. }
  1826.  
  1827. /**
  1828. * Less than (strict)
  1829. * @method lt
  1830. * @param {Object} other UINT64
  1831. * @return {Boolean}
  1832. */
  1833. UINT64.prototype.lessThan = UINT64.prototype.lt = function (other) {
  1834. if (this._a48 < other._a48) return true
  1835. if (this._a48 > other._a48) return false
  1836. if (this._a32 < other._a32) return true
  1837. if (this._a32 > other._a32) return false
  1838. if (this._a16 < other._a16) return true
  1839. if (this._a16 > other._a16) return false
  1840. return this._a00 < other._a00
  1841. }
  1842.  
  1843. /**
  1844. * Bitwise OR
  1845. * @method or
  1846. * @param {Object} other UINT64
  1847. * @return ThisExpression
  1848. */
  1849. UINT64.prototype.or = function (other) {
  1850. this._a00 |= other._a00
  1851. this._a16 |= other._a16
  1852. this._a32 |= other._a32
  1853. this._a48 |= other._a48
  1854.  
  1855. return this
  1856. }
  1857.  
  1858. /**
  1859. * Bitwise AND
  1860. * @method and
  1861. * @param {Object} other UINT64
  1862. * @return ThisExpression
  1863. */
  1864. UINT64.prototype.and = function (other) {
  1865. this._a00 &= other._a00
  1866. this._a16 &= other._a16
  1867. this._a32 &= other._a32
  1868. this._a48 &= other._a48
  1869.  
  1870. return this
  1871. }
  1872.  
  1873. /**
  1874. * Bitwise XOR
  1875. * @method xor
  1876. * @param {Object} other UINT64
  1877. * @return ThisExpression
  1878. */
  1879. UINT64.prototype.xor = function (other) {
  1880. this._a00 ^= other._a00
  1881. this._a16 ^= other._a16
  1882. this._a32 ^= other._a32
  1883. this._a48 ^= other._a48
  1884.  
  1885. return this
  1886. }
  1887.  
  1888. /**
  1889. * Bitwise NOT
  1890. * @method not
  1891. * @return ThisExpression
  1892. */
  1893. UINT64.prototype.not = function() {
  1894. this._a00 = ~this._a00 & 0xFFFF
  1895. this._a16 = ~this._a16 & 0xFFFF
  1896. this._a32 = ~this._a32 & 0xFFFF
  1897. this._a48 = ~this._a48 & 0xFFFF
  1898.  
  1899. return this
  1900. }
  1901.  
  1902. /**
  1903. * Bitwise shift right
  1904. * @method shiftRight
  1905. * @param {Number} number of bits to shift
  1906. * @return ThisExpression
  1907. */
  1908. UINT64.prototype.shiftRight = UINT64.prototype.shiftr = function (n) {
  1909. n %= 64
  1910. if (n >= 48) {
  1911. this._a00 = this._a48 >> (n - 48)
  1912. this._a16 = 0
  1913. this._a32 = 0
  1914. this._a48 = 0
  1915. } else if (n >= 32) {
  1916. n -= 32
  1917. this._a00 = ( (this._a32 >> n) | (this._a48 << (16-n)) ) & 0xFFFF
  1918. this._a16 = (this._a48 >> n) & 0xFFFF
  1919. this._a32 = 0
  1920. this._a48 = 0
  1921. } else if (n >= 16) {
  1922. n -= 16
  1923. this._a00 = ( (this._a16 >> n) | (this._a32 << (16-n)) ) & 0xFFFF
  1924. this._a16 = ( (this._a32 >> n) | (this._a48 << (16-n)) ) & 0xFFFF
  1925. this._a32 = (this._a48 >> n) & 0xFFFF
  1926. this._a48 = 0
  1927. } else {
  1928. this._a00 = ( (this._a00 >> n) | (this._a16 << (16-n)) ) & 0xFFFF
  1929. this._a16 = ( (this._a16 >> n) | (this._a32 << (16-n)) ) & 0xFFFF
  1930. this._a32 = ( (this._a32 >> n) | (this._a48 << (16-n)) ) & 0xFFFF
  1931. this._a48 = (this._a48 >> n) & 0xFFFF
  1932. }
  1933.  
  1934. return this
  1935. }
  1936.  
  1937. /**
  1938. * Bitwise shift left
  1939. * @method shiftLeft
  1940. * @param {Number} number of bits to shift
  1941. * @param {Boolean} allow overflow
  1942. * @return ThisExpression
  1943. */
  1944. UINT64.prototype.shiftLeft = UINT64.prototype.shiftl = function (n, allowOverflow) {
  1945. n %= 64
  1946. if (n >= 48) {
  1947. this._a48 = this._a00 << (n - 48)
  1948. this._a32 = 0
  1949. this._a16 = 0
  1950. this._a00 = 0
  1951. } else if (n >= 32) {
  1952. n -= 32
  1953. this._a48 = (this._a16 << n) | (this._a00 >> (16-n))
  1954. this._a32 = (this._a00 << n) & 0xFFFF
  1955. this._a16 = 0
  1956. this._a00 = 0
  1957. } else if (n >= 16) {
  1958. n -= 16
  1959. this._a48 = (this._a32 << n) | (this._a16 >> (16-n))
  1960. this._a32 = ( (this._a16 << n) | (this._a00 >> (16-n)) ) & 0xFFFF
  1961. this._a16 = (this._a00 << n) & 0xFFFF
  1962. this._a00 = 0
  1963. } else {
  1964. this._a48 = (this._a48 << n) | (this._a32 >> (16-n))
  1965. this._a32 = ( (this._a32 << n) | (this._a16 >> (16-n)) ) & 0xFFFF
  1966. this._a16 = ( (this._a16 << n) | (this._a00 >> (16-n)) ) & 0xFFFF
  1967. this._a00 = (this._a00 << n) & 0xFFFF
  1968. }
  1969. if (!allowOverflow) {
  1970. this._a48 &= 0xFFFF
  1971. }
  1972.  
  1973. return this
  1974. }
  1975.  
  1976. /**
  1977. * Bitwise rotate left
  1978. * @method rotl
  1979. * @param {Number} number of bits to rotate
  1980. * @return ThisExpression
  1981. */
  1982. UINT64.prototype.rotateLeft = UINT64.prototype.rotl = function (n) {
  1983. n %= 64
  1984. if (n == 0) return this
  1985. if (n >= 32) {
  1986. // A.B.C.D
  1987. // B.C.D.A rotl(16)
  1988. // C.D.A.B rotl(32)
  1989. var v = this._a00
  1990. this._a00 = this._a32
  1991. this._a32 = v
  1992. v = this._a48
  1993. this._a48 = this._a16
  1994. this._a16 = v
  1995. if (n == 32) return this
  1996. n -= 32
  1997. }
  1998.  
  1999. var high = (this._a48 << 16) | this._a32
  2000. var low = (this._a16 << 16) | this._a00
  2001.  
  2002. var _high = (high << n) | (low >>> (32 - n))
  2003. var _low = (low << n) | (high >>> (32 - n))
  2004.  
  2005. this._a00 = _low & 0xFFFF
  2006. this._a16 = _low >>> 16
  2007. this._a32 = _high & 0xFFFF
  2008. this._a48 = _high >>> 16
  2009.  
  2010. return this
  2011. }
  2012.  
  2013. /**
  2014. * Bitwise rotate right
  2015. * @method rotr
  2016. * @param {Number} number of bits to rotate
  2017. * @return ThisExpression
  2018. */
  2019. UINT64.prototype.rotateRight = UINT64.prototype.rotr = function (n) {
  2020. n %= 64
  2021. if (n == 0) return this
  2022. if (n >= 32) {
  2023. // A.B.C.D
  2024. // D.A.B.C rotr(16)
  2025. // C.D.A.B rotr(32)
  2026. var v = this._a00
  2027. this._a00 = this._a32
  2028. this._a32 = v
  2029. v = this._a48
  2030. this._a48 = this._a16
  2031. this._a16 = v
  2032. if (n == 32) return this
  2033. n -= 32
  2034. }
  2035.  
  2036. var high = (this._a48 << 16) | this._a32
  2037. var low = (this._a16 << 16) | this._a00
  2038.  
  2039. var _high = (high >>> n) | (low << (32 - n))
  2040. var _low = (low >>> n) | (high << (32 - n))
  2041.  
  2042. this._a00 = _low & 0xFFFF
  2043. this._a16 = _low >>> 16
  2044. this._a32 = _high & 0xFFFF
  2045. this._a48 = _high >>> 16
  2046.  
  2047. return this
  2048. }
  2049.  
  2050. /**
  2051. * Clone the current _UINT64_
  2052. * @method clone
  2053. * @return {Object} cloned UINT64
  2054. */
  2055. UINT64.prototype.clone = function () {
  2056. return new UINT64(this._a00, this._a16, this._a32, this._a48)
  2057. }
  2058.  
  2059. if (typeof define != 'undefined' && define.amd) {
  2060. // AMD / RequireJS
  2061. define([], function () {
  2062. return UINT64
  2063. })
  2064. } else if (typeof module != 'undefined' && module.exports) {
  2065. // Node.js
  2066. module.exports = UINT64
  2067. } else {
  2068. // Browser
  2069. root['UINT64'] = UINT64
  2070. }
  2071.  
  2072. })(this)
  2073.  
  2074. },{}],10:[function(require,module,exports){
  2075. (function (Buffer){
  2076. /**
  2077. xxHash implementation in pure Javascript
  2078.  
  2079. Copyright (C) 2013, Pierre Curto
  2080. MIT license
  2081. */
  2082. ;(function (root) {
  2083.  
  2084. var UINT32 = require('cuint').UINT32
  2085.  
  2086. /*
  2087. Merged this sequence of method calls as it speeds up
  2088. the calculations by a factor of 2
  2089. */
  2090. // this.v1.add( other.multiply(PRIME32_2) ).rotl(13).multiply(PRIME32_1);
  2091. UINT32.prototype.xxh_update = function (low, high) {
  2092. var b00 = PRIME32_2._low
  2093. var b16 = PRIME32_2._high
  2094.  
  2095. var c16, c00
  2096. c00 = low * b00
  2097. c16 = c00 >>> 16
  2098.  
  2099. c16 += high * b00
  2100. c16 &= 0xFFFF // Not required but improves performance
  2101. c16 += low * b16
  2102.  
  2103. var a00 = this._low + (c00 & 0xFFFF)
  2104. var a16 = a00 >>> 16
  2105.  
  2106. a16 += this._high + (c16 & 0xFFFF)
  2107.  
  2108. var v = (a16 << 16) | (a00 & 0xFFFF)
  2109. v = (v << 13) | (v >>> 19)
  2110.  
  2111. a00 = v & 0xFFFF
  2112. a16 = v >>> 16
  2113.  
  2114. b00 = PRIME32_1._low
  2115. b16 = PRIME32_1._high
  2116.  
  2117. c00 = a00 * b00
  2118. c16 = c00 >>> 16
  2119.  
  2120. c16 += a16 * b00
  2121. c16 &= 0xFFFF // Not required but improves performance
  2122. c16 += a00 * b16
  2123.  
  2124. this._low = c00 & 0xFFFF
  2125. this._high = c16 & 0xFFFF
  2126. }
  2127.  
  2128. /*
  2129. * Constants
  2130. */
  2131. var PRIME32_1 = UINT32( '2654435761' )
  2132. var PRIME32_2 = UINT32( '2246822519' )
  2133. var PRIME32_3 = UINT32( '3266489917' )
  2134. var PRIME32_4 = UINT32( '668265263' )
  2135. var PRIME32_5 = UINT32( '374761393' )
  2136.  
  2137. var PRIME32_1plus2 = PRIME32_1.clone().add(PRIME32_2)
  2138.  
  2139. /**
  2140. * Convert string to proper UTF-8 array
  2141. * @param str Input string
  2142. * @returns {Uint8Array} UTF8 array is returned as uint8 array
  2143. */
  2144. function toUTF8Array (str) {
  2145. var utf8 = []
  2146. for (var i=0, n=str.length; i < n; i++) {
  2147. var charcode = str.charCodeAt(i)
  2148. if (charcode < 0x80) utf8.push(charcode)
  2149. else if (charcode < 0x800) {
  2150. utf8.push(0xc0 | (charcode >> 6),
  2151. 0x80 | (charcode & 0x3f))
  2152. }
  2153. else if (charcode < 0xd800 || charcode >= 0xe000) {
  2154. utf8.push(0xe0 | (charcode >> 12),
  2155. 0x80 | ((charcode>>6) & 0x3f),
  2156. 0x80 | (charcode & 0x3f))
  2157. }
  2158. // surrogate pair
  2159. else {
  2160. i++;
  2161. // UTF-16 encodes 0x10000-0x10FFFF by
  2162. // subtracting 0x10000 and splitting the
  2163. // 20 bits of 0x0-0xFFFFF into two halves
  2164. charcode = 0x10000 + (((charcode & 0x3ff)<<10)
  2165. | (str.charCodeAt(i) & 0x3ff))
  2166. utf8.push(0xf0 | (charcode >>18),
  2167. 0x80 | ((charcode>>12) & 0x3f),
  2168. 0x80 | ((charcode>>6) & 0x3f),
  2169. 0x80 | (charcode & 0x3f))
  2170. }
  2171. }
  2172.  
  2173. return new Uint8Array(utf8)
  2174. }
  2175.  
  2176. /**
  2177. * XXH object used as a constructor or a function
  2178. * @constructor
  2179. * or
  2180. * @param {Object|String} input data
  2181. * @param {Number|UINT32} seed
  2182. * @return ThisExpression
  2183. * or
  2184. * @return {UINT32} xxHash
  2185. */
  2186. function XXH () {
  2187. if (arguments.length == 2)
  2188. return new XXH( arguments[1] ).update( arguments[0] ).digest()
  2189.  
  2190. if (!(this instanceof XXH))
  2191. return new XXH( arguments[0] )
  2192.  
  2193. init.call(this, arguments[0])
  2194. }
  2195.  
  2196. /**
  2197. * Initialize the XXH instance with the given seed
  2198. * @method init
  2199. * @param {Number|Object} seed as a number or an unsigned 32 bits integer
  2200. * @return ThisExpression
  2201. */
  2202. function init (seed) {
  2203. this.seed = seed instanceof UINT32 ? seed.clone() : UINT32(seed)
  2204. this.v1 = this.seed.clone().add(PRIME32_1plus2)
  2205. this.v2 = this.seed.clone().add(PRIME32_2)
  2206. this.v3 = this.seed.clone()
  2207. this.v4 = this.seed.clone().subtract(PRIME32_1)
  2208. this.total_len = 0
  2209. this.memsize = 0
  2210. this.memory = null
  2211.  
  2212. return this
  2213. }
  2214. XXH.prototype.init = init
  2215.  
  2216. /**
  2217. * Add data to be computed for the XXH hash
  2218. * @method update
  2219. * @param {String|Buffer|ArrayBuffer} input as a string or nodejs Buffer or ArrayBuffer
  2220. * @return ThisExpression
  2221. */
  2222. XXH.prototype.update = function (input) {
  2223. var isString = typeof input == 'string'
  2224. var isArrayBuffer
  2225.  
  2226. // Convert all strings to utf-8 first (issue #5)
  2227. if (isString) {
  2228. input = toUTF8Array(input)
  2229. isString = false
  2230. isArrayBuffer = true
  2231. }
  2232.  
  2233. if (typeof ArrayBuffer !== "undefined" && input instanceof ArrayBuffer)
  2234. {
  2235. isArrayBuffer = true
  2236. input = new Uint8Array(input);
  2237. }
  2238.  
  2239. var p = 0
  2240. var len = input.length
  2241. var bEnd = p + len
  2242.  
  2243. if (len == 0) return this
  2244.  
  2245. this.total_len += len
  2246.  
  2247. if (this.memsize == 0)
  2248. {
  2249. if (isString) {
  2250. this.memory = ''
  2251. } else if (isArrayBuffer) {
  2252. this.memory = new Uint8Array(16)
  2253. } else {
  2254. this.memory = new Buffer(16)
  2255. }
  2256. }
  2257.  
  2258. if (this.memsize + len < 16) // fill in tmp buffer
  2259. {
  2260. // XXH_memcpy(this.memory + this.memsize, input, len)
  2261. if (isString) {
  2262. this.memory += input
  2263. } else if (isArrayBuffer) {
  2264. this.memory.set( input.subarray(0, len), this.memsize )
  2265. } else {
  2266. input.copy( this.memory, this.memsize, 0, len )
  2267. }
  2268.  
  2269. this.memsize += len
  2270. return this
  2271. }
  2272.  
  2273. if (this.memsize > 0) // some data left from previous update
  2274. {
  2275. // XXH_memcpy(this.memory + this.memsize, input, 16-this.memsize);
  2276. if (isString) {
  2277. this.memory += input.slice(0, 16 - this.memsize)
  2278. } else if (isArrayBuffer) {
  2279. this.memory.set( input.subarray(0, 16 - this.memsize), this.memsize )
  2280. } else {
  2281. input.copy( this.memory, this.memsize, 0, 16 - this.memsize )
  2282. }
  2283.  
  2284. var p32 = 0
  2285. if (isString) {
  2286. this.v1.xxh_update(
  2287. (this.memory.charCodeAt(p32+1) << 8) | this.memory.charCodeAt(p32)
  2288. , (this.memory.charCodeAt(p32+3) << 8) | this.memory.charCodeAt(p32+2)
  2289. )
  2290. p32 += 4
  2291. this.v2.xxh_update(
  2292. (this.memory.charCodeAt(p32+1) << 8) | this.memory.charCodeAt(p32)
  2293. , (this.memory.charCodeAt(p32+3) << 8) | this.memory.charCodeAt(p32+2)
  2294. )
  2295. p32 += 4
  2296. this.v3.xxh_update(
  2297. (this.memory.charCodeAt(p32+1) << 8) | this.memory.charCodeAt(p32)
  2298. , (this.memory.charCodeAt(p32+3) << 8) | this.memory.charCodeAt(p32+2)
  2299. )
  2300. p32 += 4
  2301. this.v4.xxh_update(
  2302. (this.memory.charCodeAt(p32+1) << 8) | this.memory.charCodeAt(p32)
  2303. , (this.memory.charCodeAt(p32+3) << 8) | this.memory.charCodeAt(p32+2)
  2304. )
  2305. } else {
  2306. this.v1.xxh_update(
  2307. (this.memory[p32+1] << 8) | this.memory[p32]
  2308. , (this.memory[p32+3] << 8) | this.memory[p32+2]
  2309. )
  2310. p32 += 4
  2311. this.v2.xxh_update(
  2312. (this.memory[p32+1] << 8) | this.memory[p32]
  2313. , (this.memory[p32+3] << 8) | this.memory[p32+2]
  2314. )
  2315. p32 += 4
  2316. this.v3.xxh_update(
  2317. (this.memory[p32+1] << 8) | this.memory[p32]
  2318. , (this.memory[p32+3] << 8) | this.memory[p32+2]
  2319. )
  2320. p32 += 4
  2321. this.v4.xxh_update(
  2322. (this.memory[p32+1] << 8) | this.memory[p32]
  2323. , (this.memory[p32+3] << 8) | this.memory[p32+2]
  2324. )
  2325. }
  2326.  
  2327. p += 16 - this.memsize
  2328. this.memsize = 0
  2329. if (isString) this.memory = ''
  2330. }
  2331.  
  2332. if (p <= bEnd - 16)
  2333. {
  2334. var limit = bEnd - 16
  2335.  
  2336. do
  2337. {
  2338. if (isString) {
  2339. this.v1.xxh_update(
  2340. (input.charCodeAt(p+1) << 8) | input.charCodeAt(p)
  2341. , (input.charCodeAt(p+3) << 8) | input.charCodeAt(p+2)
  2342. )
  2343. p += 4
  2344. this.v2.xxh_update(
  2345. (input.charCodeAt(p+1) << 8) | input.charCodeAt(p)
  2346. , (input.charCodeAt(p+3) << 8) | input.charCodeAt(p+2)
  2347. )
  2348. p += 4
  2349. this.v3.xxh_update(
  2350. (input.charCodeAt(p+1) << 8) | input.charCodeAt(p)
  2351. , (input.charCodeAt(p+3) << 8) | input.charCodeAt(p+2)
  2352. )
  2353. p += 4
  2354. this.v4.xxh_update(
  2355. (input.charCodeAt(p+1) << 8) | input.charCodeAt(p)
  2356. , (input.charCodeAt(p+3) << 8) | input.charCodeAt(p+2)
  2357. )
  2358. } else {
  2359. this.v1.xxh_update(
  2360. (input[p+1] << 8) | input[p]
  2361. , (input[p+3] << 8) | input[p+2]
  2362. )
  2363. p += 4
  2364. this.v2.xxh_update(
  2365. (input[p+1] << 8) | input[p]
  2366. , (input[p+3] << 8) | input[p+2]
  2367. )
  2368. p += 4
  2369. this.v3.xxh_update(
  2370. (input[p+1] << 8) | input[p]
  2371. , (input[p+3] << 8) | input[p+2]
  2372. )
  2373. p += 4
  2374. this.v4.xxh_update(
  2375. (input[p+1] << 8) | input[p]
  2376. , (input[p+3] << 8) | input[p+2]
  2377. )
  2378. }
  2379. p += 4
  2380. } while (p <= limit)
  2381. }
  2382.  
  2383. if (p < bEnd)
  2384. {
  2385. // XXH_memcpy(this.memory, p, bEnd-p);
  2386. if (isString) {
  2387. this.memory += input.slice(p)
  2388. } else if (isArrayBuffer) {
  2389. this.memory.set( input.subarray(p, bEnd), this.memsize )
  2390. } else {
  2391. input.copy( this.memory, this.memsize, p, bEnd )
  2392. }
  2393.  
  2394. this.memsize = bEnd - p
  2395. }
  2396.  
  2397. return this
  2398. }
  2399.  
  2400. /**
  2401. * Finalize the XXH computation. The XXH instance is ready for reuse for the given seed
  2402. * @method digest
  2403. * @return {UINT32} xxHash
  2404. */
  2405. XXH.prototype.digest = function () {
  2406. var input = this.memory
  2407. var isString = typeof input == 'string'
  2408. var p = 0
  2409. var bEnd = this.memsize
  2410. var h32, h
  2411. var u = new UINT32
  2412.  
  2413. if (this.total_len >= 16)
  2414. {
  2415. h32 = this.v1.rotl(1).add( this.v2.rotl(7).add( this.v3.rotl(12).add( this.v4.rotl(18) ) ) )
  2416. }
  2417. else
  2418. {
  2419. h32 = this.seed.add( PRIME32_5 )
  2420. }
  2421.  
  2422. h32.add( u.fromNumber(this.total_len) )
  2423.  
  2424. while (p <= bEnd - 4)
  2425. {
  2426. if (isString) {
  2427. u.fromBits(
  2428. (input.charCodeAt(p+1) << 8) | input.charCodeAt(p)
  2429. , (input.charCodeAt(p+3) << 8) | input.charCodeAt(p+2)
  2430. )
  2431. } else {
  2432. u.fromBits(
  2433. (input[p+1] << 8) | input[p]
  2434. , (input[p+3] << 8) | input[p+2]
  2435. )
  2436. }
  2437. h32
  2438. .add( u.multiply(PRIME32_3) )
  2439. .rotl(17)
  2440. .multiply( PRIME32_4 )
  2441. p += 4
  2442. }
  2443.  
  2444. while (p < bEnd)
  2445. {
  2446. u.fromBits( isString ? input.charCodeAt(p++) : input[p++], 0 )
  2447. h32
  2448. .add( u.multiply(PRIME32_5) )
  2449. .rotl(11)
  2450. .multiply(PRIME32_1)
  2451. }
  2452.  
  2453. h = h32.clone().shiftRight(15)
  2454. h32.xor(h).multiply(PRIME32_2)
  2455.  
  2456. h = h32.clone().shiftRight(13)
  2457. h32.xor(h).multiply(PRIME32_3)
  2458.  
  2459. h = h32.clone().shiftRight(16)
  2460. h32.xor(h)
  2461.  
  2462. // Reset the state
  2463. this.init( this.seed )
  2464.  
  2465. return h32
  2466. }
  2467.  
  2468. if (typeof define != 'undefined' && define.amd) {
  2469. // AMD / RequireJS
  2470. define([], function () {
  2471. return XXH
  2472. })
  2473. } else if (typeof module != 'undefined' && module.exports) {
  2474. // Node.js
  2475. module.exports = XXH
  2476. } else {
  2477. // Browser
  2478. root['XXH'] = XXH
  2479. }
  2480.  
  2481. })(this)
  2482.  
  2483. }).call(this,require("buffer").Buffer)
  2484. },{"buffer":"buffer","cuint":7}],11:[function(require,module,exports){
  2485.  
  2486. },{}],12:[function(require,module,exports){
  2487. var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  2488.  
  2489. ;(function (exports) {
  2490. 'use strict';
  2491.  
  2492. var Arr = (typeof Uint8Array !== 'undefined')
  2493. ? Uint8Array
  2494. : Array
  2495.  
  2496. var PLUS = '+'.charCodeAt(0)
  2497. var SLASH = '/'.charCodeAt(0)
  2498. var NUMBER = '0'.charCodeAt(0)
  2499. var LOWER = 'a'.charCodeAt(0)
  2500. var UPPER = 'A'.charCodeAt(0)
  2501. var PLUS_URL_SAFE = '-'.charCodeAt(0)
  2502. var SLASH_URL_SAFE = '_'.charCodeAt(0)
  2503.  
  2504. function decode (elt) {
  2505. var code = elt.charCodeAt(0)
  2506. if (code === PLUS ||
  2507. code === PLUS_URL_SAFE)
  2508. return 62 // '+'
  2509. if (code === SLASH ||
  2510. code === SLASH_URL_SAFE)
  2511. return 63 // '/'
  2512. if (code < NUMBER)
  2513. return -1 //no match
  2514. if (code < NUMBER + 10)
  2515. return code - NUMBER + 26 + 26
  2516. if (code < UPPER + 26)
  2517. return code - UPPER
  2518. if (code < LOWER + 26)
  2519. return code - LOWER + 26
  2520. }
  2521.  
  2522. function b64ToByteArray (b64) {
  2523. var i, j, l, tmp, placeHolders, arr
  2524.  
  2525. if (b64.length % 4 > 0) {
  2526. throw new Error('Invalid string. Length must be a multiple of 4')
  2527. }
  2528.  
  2529. // the number of equal signs (place holders)
  2530. // if there are two placeholders, than the two characters before it
  2531. // represent one byte
  2532. // if there is only one, then the three characters before it represent 2 bytes
  2533. // this is just a cheap hack to not do indexOf twice
  2534. var len = b64.length
  2535. placeHolders = '=' === b64.charAt(len - 2) ? 2 : '=' === b64.charAt(len - 1) ? 1 : 0
  2536.  
  2537. // base64 is 4/3 + up to two characters of the original data
  2538. arr = new Arr(b64.length * 3 / 4 - placeHolders)
  2539.  
  2540. // if there are placeholders, only get up to the last complete 4 chars
  2541. l = placeHolders > 0 ? b64.length - 4 : b64.length
  2542.  
  2543. var L = 0
  2544.  
  2545. function push (v) {
  2546. arr[L++] = v
  2547. }
  2548.  
  2549. for (i = 0, j = 0; i < l; i += 4, j += 3) {
  2550. tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3))
  2551. push((tmp & 0xFF0000) >> 16)
  2552. push((tmp & 0xFF00) >> 8)
  2553. push(tmp & 0xFF)
  2554. }
  2555.  
  2556. if (placeHolders === 2) {
  2557. tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4)
  2558. push(tmp & 0xFF)
  2559. } else if (placeHolders === 1) {
  2560. tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2)
  2561. push((tmp >> 8) & 0xFF)
  2562. push(tmp & 0xFF)
  2563. }
  2564.  
  2565. return arr
  2566. }
  2567.  
  2568. function uint8ToBase64 (uint8) {
  2569. var i,
  2570. extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
  2571. output = "",
  2572. temp, length
  2573.  
  2574. function encode (num) {
  2575. return lookup.charAt(num)
  2576. }
  2577.  
  2578. function tripletToBase64 (num) {
  2579. return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F)
  2580. }
  2581.  
  2582. // go through the array every three bytes, we'll deal with trailing stuff later
  2583. for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
  2584. temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
  2585. output += tripletToBase64(temp)
  2586. }
  2587.  
  2588. // pad the end with zeros, but make sure to not forget the extra bytes
  2589. switch (extraBytes) {
  2590. case 1:
  2591. temp = uint8[uint8.length - 1]
  2592. output += encode(temp >> 2)
  2593. output += encode((temp << 4) & 0x3F)
  2594. output += '=='
  2595. break
  2596. case 2:
  2597. temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])
  2598. output += encode(temp >> 10)
  2599. output += encode((temp >> 4) & 0x3F)
  2600. output += encode((temp << 2) & 0x3F)
  2601. output += '='
  2602. break
  2603. }
  2604.  
  2605. return output
  2606. }
  2607.  
  2608. exports.toByteArray = b64ToByteArray
  2609. exports.fromByteArray = uint8ToBase64
  2610. }(typeof exports === 'undefined' ? (this.base64js = {}) : exports))
  2611.  
  2612. },{}],13:[function(require,module,exports){
  2613. exports.read = function (buffer, offset, isLE, mLen, nBytes) {
  2614. var e, m
  2615. var eLen = nBytes * 8 - mLen - 1
  2616. var eMax = (1 << eLen) - 1
  2617. var eBias = eMax >> 1
  2618. var nBits = -7
  2619. var i = isLE ? (nBytes - 1) : 0
  2620. var d = isLE ? -1 : 1
  2621. var s = buffer[offset + i]
  2622.  
  2623. i += d
  2624.  
  2625. e = s & ((1 << (-nBits)) - 1)
  2626. s >>= (-nBits)
  2627. nBits += eLen
  2628. for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  2629.  
  2630. m = e & ((1 << (-nBits)) - 1)
  2631. e >>= (-nBits)
  2632. nBits += mLen
  2633. for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  2634.  
  2635. if (e === 0) {
  2636. e = 1 - eBias
  2637. } else if (e === eMax) {
  2638. return m ? NaN : ((s ? -1 : 1) * Infinity)
  2639. } else {
  2640. m = m + Math.pow(2, mLen)
  2641. e = e - eBias
  2642. }
  2643. return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
  2644. }
  2645.  
  2646. exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
  2647. var e, m, c
  2648. var eLen = nBytes * 8 - mLen - 1
  2649. var eMax = (1 << eLen) - 1
  2650. var eBias = eMax >> 1
  2651. var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
  2652. var i = isLE ? 0 : (nBytes - 1)
  2653. var d = isLE ? 1 : -1
  2654. var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
  2655.  
  2656. value = Math.abs(value)
  2657.  
  2658. if (isNaN(value) || value === Infinity) {
  2659. m = isNaN(value) ? 1 : 0
  2660. e = eMax
  2661. } else {
  2662. e = Math.floor(Math.log(value) / Math.LN2)
  2663. if (value * (c = Math.pow(2, -e)) < 1) {
  2664. e--
  2665. c *= 2
  2666. }
  2667. if (e + eBias >= 1) {
  2668. value += rt / c
  2669. } else {
  2670. value += rt * Math.pow(2, 1 - eBias)
  2671. }
  2672. if (value * c >= 2) {
  2673. e++
  2674. c /= 2
  2675. }
  2676.  
  2677. if (e + eBias >= eMax) {
  2678. m = 0
  2679. e = eMax
  2680. } else if (e + eBias >= 1) {
  2681. m = (value * c - 1) * Math.pow(2, mLen)
  2682. e = e + eBias
  2683. } else {
  2684. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
  2685. e = 0
  2686. }
  2687. }
  2688.  
  2689. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
  2690.  
  2691. e = (e << mLen) | m
  2692. eLen += mLen
  2693. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
  2694.  
  2695. buffer[offset + i - d] |= s * 128
  2696. }
  2697.  
  2698. },{}],14:[function(require,module,exports){
  2699.  
  2700. /**
  2701. * isArray
  2702. */
  2703.  
  2704. var isArray = Array.isArray;
  2705.  
  2706. /**
  2707. * toString
  2708. */
  2709.  
  2710. var str = Object.prototype.toString;
  2711.  
  2712. /**
  2713. * Whether or not the given `val`
  2714. * is an array.
  2715. *
  2716. * example:
  2717. *
  2718. * isArray([]);
  2719. * // > true
  2720. * isArray(arguments);
  2721. * // > false
  2722. * isArray('');
  2723. * // > false
  2724. *
  2725. * @param {mixed} val
  2726. * @return {bool}
  2727. */
  2728.  
  2729. module.exports = isArray || function (val) {
  2730. return !! val && '[object Array]' == str.call(val);
  2731. };
  2732.  
  2733. },{}],15:[function(require,module,exports){
  2734. // Copyright Joyent, Inc. and other Node contributors.
  2735. //
  2736. // Permission is hereby granted, free of charge, to any person obtaining a
  2737. // copy of this software and associated documentation files (the
  2738. // "Software"), to deal in the Software without restriction, including
  2739. // without limitation the rights to use, copy, modify, merge, publish,
  2740. // distribute, sublicense, and/or sell copies of the Software, and to permit
  2741. // persons to whom the Software is furnished to do so, subject to the
  2742. // following conditions:
  2743. //
  2744. // The above copyright notice and this permission notice shall be included
  2745. // in all copies or substantial portions of the Software.
  2746. //
  2747. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  2748. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2749. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  2750. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  2751. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  2752. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  2753. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  2754.  
  2755. function EventEmitter() {
  2756. this._events = this._events || {};
  2757. this._maxListeners = this._maxListeners || undefined;
  2758. }
  2759. module.exports = EventEmitter;
  2760.  
  2761. // Backwards-compat with node 0.10.x
  2762. EventEmitter.EventEmitter = EventEmitter;
  2763.  
  2764. EventEmitter.prototype._events = undefined;
  2765. EventEmitter.prototype._maxListeners = undefined;
  2766.  
  2767. // By default EventEmitters will print a warning if more than 10 listeners are
  2768. // added to it. This is a useful default which helps finding memory leaks.
  2769. EventEmitter.defaultMaxListeners = 10;
  2770.  
  2771. // Obviously not all Emitters should be limited to 10. This function allows
  2772. // that to be increased. Set to zero for unlimited.
  2773. EventEmitter.prototype.setMaxListeners = function(n) {
  2774. if (!isNumber(n) || n < 0 || isNaN(n))
  2775. throw TypeError('n must be a positive number');
  2776. this._maxListeners = n;
  2777. return this;
  2778. };
  2779.  
  2780. EventEmitter.prototype.emit = function(type) {
  2781. var er, handler, len, args, i, listeners;
  2782.  
  2783. if (!this._events)
  2784. this._events = {};
  2785.  
  2786. // If there is no 'error' event listener then throw.
  2787. if (type === 'error') {
  2788. if (!this._events.error ||
  2789. (isObject(this._events.error) && !this._events.error.length)) {
  2790. er = arguments[1];
  2791. if (er instanceof Error) {
  2792. throw er; // Unhandled 'error' event
  2793. }
  2794. throw TypeError('Uncaught, unspecified "error" event.');
  2795. }
  2796. }
  2797.  
  2798. handler = this._events[type];
  2799.  
  2800. if (isUndefined(handler))
  2801. return false;
  2802.  
  2803. if (isFunction(handler)) {
  2804. switch (arguments.length) {
  2805. // fast cases
  2806. case 1:
  2807. handler.call(this);
  2808. break;
  2809. case 2:
  2810. handler.call(this, arguments[1]);
  2811. break;
  2812. case 3:
  2813. handler.call(this, arguments[1], arguments[2]);
  2814. break;
  2815. // slower
  2816. default:
  2817. args = Array.prototype.slice.call(arguments, 1);
  2818. handler.apply(this, args);
  2819. }
  2820. } else if (isObject(handler)) {
  2821. args = Array.prototype.slice.call(arguments, 1);
  2822. listeners = handler.slice();
  2823. len = listeners.length;
  2824. for (i = 0; i < len; i++)
  2825. listeners[i].apply(this, args);
  2826. }
  2827.  
  2828. return true;
  2829. };
  2830.  
  2831. EventEmitter.prototype.addListener = function(type, listener) {
  2832. var m;
  2833.  
  2834. if (!isFunction(listener))
  2835. throw TypeError('listener must be a function');
  2836.  
  2837. if (!this._events)
  2838. this._events = {};
  2839.  
  2840. // To avoid recursion in the case that type === "newListener"! Before
  2841. // adding it to the listeners, first emit "newListener".
  2842. if (this._events.newListener)
  2843. this.emit('newListener', type,
  2844. isFunction(listener.listener) ?
  2845. listener.listener : listener);
  2846.  
  2847. if (!this._events[type])
  2848. // Optimize the case of one listener. Don't need the extra array object.
  2849. this._events[type] = listener;
  2850. else if (isObject(this._events[type]))
  2851. // If we've already got an array, just append.
  2852. this._events[type].push(listener);
  2853. else
  2854. // Adding the second element, need to change to array.
  2855. this._events[type] = [this._events[type], listener];
  2856.  
  2857. // Check for listener leak
  2858. if (isObject(this._events[type]) && !this._events[type].warned) {
  2859. if (!isUndefined(this._maxListeners)) {
  2860. m = this._maxListeners;
  2861. } else {
  2862. m = EventEmitter.defaultMaxListeners;
  2863. }
  2864.  
  2865. if (m && m > 0 && this._events[type].length > m) {
  2866. this._events[type].warned = true;
  2867. console.error('(node) warning: possible EventEmitter memory ' +
  2868. 'leak detected. %d listeners added. ' +
  2869. 'Use emitter.setMaxListeners() to increase limit.',
  2870. this._events[type].length);
  2871. if (typeof console.trace === 'function') {
  2872. // not supported in IE 10
  2873. console.trace();
  2874. }
  2875. }
  2876. }
  2877.  
  2878. return this;
  2879. };
  2880.  
  2881. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  2882.  
  2883. EventEmitter.prototype.once = function(type, listener) {
  2884. if (!isFunction(listener))
  2885. throw TypeError('listener must be a function');
  2886.  
  2887. var fired = false;
  2888.  
  2889. function g() {
  2890. this.removeListener(type, g);
  2891.  
  2892. if (!fired) {
  2893. fired = true;
  2894. listener.apply(this, arguments);
  2895. }
  2896. }
  2897.  
  2898. g.listener = listener;
  2899. this.on(type, g);
  2900.  
  2901. return this;
  2902. };
  2903.  
  2904. // emits a 'removeListener' event iff the listener was removed
  2905. EventEmitter.prototype.removeListener = function(type, listener) {
  2906. var list, position, length, i;
  2907.  
  2908. if (!isFunction(listener))
  2909. throw TypeError('listener must be a function');
  2910.  
  2911. if (!this._events || !this._events[type])
  2912. return this;
  2913.  
  2914. list = this._events[type];
  2915. length = list.length;
  2916. position = -1;
  2917.  
  2918. if (list === listener ||
  2919. (isFunction(list.listener) && list.listener === listener)) {
  2920. delete this._events[type];
  2921. if (this._events.removeListener)
  2922. this.emit('removeListener', type, listener);
  2923.  
  2924. } else if (isObject(list)) {
  2925. for (i = length; i-- > 0;) {
  2926. if (list[i] === listener ||
  2927. (list[i].listener && list[i].listener === listener)) {
  2928. position = i;
  2929. break;
  2930. }
  2931. }
  2932.  
  2933. if (position < 0)
  2934. return this;
  2935.  
  2936. if (list.length === 1) {
  2937. list.length = 0;
  2938. delete this._events[type];
  2939. } else {
  2940. list.splice(position, 1);
  2941. }
  2942.  
  2943. if (this._events.removeListener)
  2944. this.emit('removeListener', type, listener);
  2945. }
  2946.  
  2947. return this;
  2948. };
  2949.  
  2950. EventEmitter.prototype.removeAllListeners = function(type) {
  2951. var key, listeners;
  2952.  
  2953. if (!this._events)
  2954. return this;
  2955.  
  2956. // not listening for removeListener, no need to emit
  2957. if (!this._events.removeListener) {
  2958. if (arguments.length === 0)
  2959. this._events = {};
  2960. else if (this._events[type])
  2961. delete this._events[type];
  2962. return this;
  2963. }
  2964.  
  2965. // emit removeListener for all listeners on all events
  2966. if (arguments.length === 0) {
  2967. for (key in this._events) {
  2968. if (key === 'removeListener') continue;
  2969. this.removeAllListeners(key);
  2970. }
  2971. this.removeAllListeners('removeListener');
  2972. this._events = {};
  2973. return this;
  2974. }
  2975.  
  2976. listeners = this._events[type];
  2977.  
  2978. if (isFunction(listeners)) {
  2979. this.removeListener(type, listeners);
  2980. } else if (listeners) {
  2981. // LIFO order
  2982. while (listeners.length)
  2983. this.removeListener(type, listeners[listeners.length - 1]);
  2984. }
  2985. delete this._events[type];
  2986.  
  2987. return this;
  2988. };
  2989.  
  2990. EventEmitter.prototype.listeners = function(type) {
  2991. var ret;
  2992. if (!this._events || !this._events[type])
  2993. ret = [];
  2994. else if (isFunction(this._events[type]))
  2995. ret = [this._events[type]];
  2996. else
  2997. ret = this._events[type].slice();
  2998. return ret;
  2999. };
  3000.  
  3001. EventEmitter.prototype.listenerCount = function(type) {
  3002. if (this._events) {
  3003. var evlistener = this._events[type];
  3004.  
  3005. if (isFunction(evlistener))
  3006. return 1;
  3007. else if (evlistener)
  3008. return evlistener.length;
  3009. }
  3010. return 0;
  3011. };
  3012.  
  3013. EventEmitter.listenerCount = function(emitter, type) {
  3014. return emitter.listenerCount(type);
  3015. };
  3016.  
  3017. function isFunction(arg) {
  3018. return typeof arg === 'function';
  3019. }
  3020.  
  3021. function isNumber(arg) {
  3022. return typeof arg === 'number';
  3023. }
  3024.  
  3025. function isObject(arg) {
  3026. return typeof arg === 'object' && arg !== null;
  3027. }
  3028.  
  3029. function isUndefined(arg) {
  3030. return arg === void 0;
  3031. }
  3032.  
  3033. },{}],16:[function(require,module,exports){
  3034. if (typeof Object.create === 'function') {
  3035. // implementation from standard node.js 'util' module
  3036. module.exports = function inherits(ctor, superCtor) {
  3037. ctor.super_ = superCtor
  3038. ctor.prototype = Object.create(superCtor.prototype, {
  3039. constructor: {
  3040. value: ctor,
  3041. enumerable: false,
  3042. writable: true,
  3043. configurable: true
  3044. }
  3045. });
  3046. };
  3047. } else {
  3048. // old school shim for old browsers
  3049. module.exports = function inherits(ctor, superCtor) {
  3050. ctor.super_ = superCtor
  3051. var TempCtor = function () {}
  3052. TempCtor.prototype = superCtor.prototype
  3053. ctor.prototype = new TempCtor()
  3054. ctor.prototype.constructor = ctor
  3055. }
  3056. }
  3057.  
  3058. },{}],17:[function(require,module,exports){
  3059. /**
  3060. * Determine if an object is Buffer
  3061. *
  3062. * Author: Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
  3063. * License: MIT
  3064. *
  3065. * `npm install is-buffer`
  3066. */
  3067.  
  3068. module.exports = function (obj) {
  3069. return !!(obj != null &&
  3070. (obj._isBuffer || // For Safari 5-7 (missing Object.prototype.constructor)
  3071. (obj.constructor &&
  3072. typeof obj.constructor.isBuffer === 'function' &&
  3073. obj.constructor.isBuffer(obj))
  3074. ))
  3075. }
  3076.  
  3077. },{}],18:[function(require,module,exports){
  3078. module.exports = Array.isArray || function (arr) {
  3079. return Object.prototype.toString.call(arr) == '[object Array]';
  3080. };
  3081.  
  3082. },{}],19:[function(require,module,exports){
  3083. // shim for using process in browser
  3084.  
  3085. var process = module.exports = {};
  3086. var queue = [];
  3087. var draining = false;
  3088. var currentQueue;
  3089. var queueIndex = -1;
  3090.  
  3091. function cleanUpNextTick() {
  3092. draining = false;
  3093. if (currentQueue.length) {
  3094. queue = currentQueue.concat(queue);
  3095. } else {
  3096. queueIndex = -1;
  3097. }
  3098. if (queue.length) {
  3099. drainQueue();
  3100. }
  3101. }
  3102.  
  3103. function drainQueue() {
  3104. if (draining) {
  3105. return;
  3106. }
  3107. var timeout = setTimeout(cleanUpNextTick);
  3108. draining = true;
  3109.  
  3110. var len = queue.length;
  3111. while(len) {
  3112. currentQueue = queue;
  3113. queue = [];
  3114. while (++queueIndex < len) {
  3115. if (currentQueue) {
  3116. currentQueue[queueIndex].run();
  3117. }
  3118. }
  3119. queueIndex = -1;
  3120. len = queue.length;
  3121. }
  3122. currentQueue = null;
  3123. draining = false;
  3124. clearTimeout(timeout);
  3125. }
  3126.  
  3127. process.nextTick = function (fun) {
  3128. var args = new Array(arguments.length - 1);
  3129. if (arguments.length > 1) {
  3130. for (var i = 1; i < arguments.length; i++) {
  3131. args[i - 1] = arguments[i];
  3132. }
  3133. }
  3134. queue.push(new Item(fun, args));
  3135. if (queue.length === 1 && !draining) {
  3136. setTimeout(drainQueue, 0);
  3137. }
  3138. };
  3139.  
  3140. // v8 likes predictible objects
  3141. function Item(fun, array) {
  3142. this.fun = fun;
  3143. this.array = array;
  3144. }
  3145. Item.prototype.run = function () {
  3146. this.fun.apply(null, this.array);
  3147. };
  3148. process.title = 'browser';
  3149. process.browser = true;
  3150. process.env = {};
  3151. process.argv = [];
  3152. process.version = ''; // empty string to avoid regexp issues
  3153. process.versions = {};
  3154.  
  3155. function noop() {}
  3156.  
  3157. process.on = noop;
  3158. process.addListener = noop;
  3159. process.once = noop;
  3160. process.off = noop;
  3161. process.removeListener = noop;
  3162. process.removeAllListeners = noop;
  3163. process.emit = noop;
  3164.  
  3165. process.binding = function (name) {
  3166. throw new Error('process.binding is not supported');
  3167. };
  3168.  
  3169. process.cwd = function () { return '/' };
  3170. process.chdir = function (dir) {
  3171. throw new Error('process.chdir is not supported');
  3172. };
  3173. process.umask = function() { return 0; };
  3174.  
  3175. },{}],20:[function(require,module,exports){
  3176. module.exports = require("./lib/_stream_duplex.js")
  3177.  
  3178. },{"./lib/_stream_duplex.js":21}],21:[function(require,module,exports){
  3179. // a duplex stream is just a stream that is both readable and writable.
  3180. // Since JS doesn't have multiple prototypal inheritance, this class
  3181. // prototypally inherits from Readable, and then parasitically from
  3182. // Writable.
  3183.  
  3184. 'use strict';
  3185.  
  3186. /*<replacement>*/
  3187. var objectKeys = Object.keys || function (obj) {
  3188. var keys = [];
  3189. for (var key in obj) keys.push(key);
  3190. return keys;
  3191. }
  3192. /*</replacement>*/
  3193.  
  3194.  
  3195. module.exports = Duplex;
  3196.  
  3197. /*<replacement>*/
  3198. var processNextTick = require('process-nextick-args');
  3199. /*</replacement>*/
  3200.  
  3201.  
  3202.  
  3203. /*<replacement>*/
  3204. var util = require('core-util-is');
  3205. util.inherits = require('inherits');
  3206. /*</replacement>*/
  3207.  
  3208. var Readable = require('./_stream_readable');
  3209. var Writable = require('./_stream_writable');
  3210.  
  3211. util.inherits(Duplex, Readable);
  3212.  
  3213. var keys = objectKeys(Writable.prototype);
  3214. for (var v = 0; v < keys.length; v++) {
  3215. var method = keys[v];
  3216. if (!Duplex.prototype[method])
  3217. Duplex.prototype[method] = Writable.prototype[method];
  3218. }
  3219.  
  3220. function Duplex(options) {
  3221. if (!(this instanceof Duplex))
  3222. return new Duplex(options);
  3223.  
  3224. Readable.call(this, options);
  3225. Writable.call(this, options);
  3226.  
  3227. if (options && options.readable === false)
  3228. this.readable = false;
  3229.  
  3230. if (options && options.writable === false)
  3231. this.writable = false;
  3232.  
  3233. this.allowHalfOpen = true;
  3234. if (options && options.allowHalfOpen === false)
  3235. this.allowHalfOpen = false;
  3236.  
  3237. this.once('end', onend);
  3238. }
  3239.  
  3240. // the no-half-open enforcer
  3241. function onend() {
  3242. // if we allow half-open state, or if the writable side ended,
  3243. // then we're ok.
  3244. if (this.allowHalfOpen || this._writableState.ended)
  3245. return;
  3246.  
  3247. // no more data can be written.
  3248. // But allow more writes to happen in this tick.
  3249. processNextTick(onEndNT, this);
  3250. }
  3251.  
  3252. function onEndNT(self) {
  3253. self.end();
  3254. }
  3255.  
  3256. function forEach (xs, f) {
  3257. for (var i = 0, l = xs.length; i < l; i++) {
  3258. f(xs[i], i);
  3259. }
  3260. }
  3261.  
  3262. },{"./_stream_readable":23,"./_stream_writable":25,"core-util-is":26,"inherits":16,"process-nextick-args":27}],22:[function(require,module,exports){
  3263. // a passthrough stream.
  3264. // basically just the most minimal sort of Transform stream.
  3265. // Every written chunk gets output as-is.
  3266.  
  3267. 'use strict';
  3268.  
  3269. module.exports = PassThrough;
  3270.  
  3271. var Transform = require('./_stream_transform');
  3272.  
  3273. /*<replacement>*/
  3274. var util = require('core-util-is');
  3275. util.inherits = require('inherits');
  3276. /*</replacement>*/
  3277.  
  3278. util.inherits(PassThrough, Transform);
  3279.  
  3280. function PassThrough(options) {
  3281. if (!(this instanceof PassThrough))
  3282. return new PassThrough(options);
  3283.  
  3284. Transform.call(this, options);
  3285. }
  3286.  
  3287. PassThrough.prototype._transform = function(chunk, encoding, cb) {
  3288. cb(null, chunk);
  3289. };
  3290.  
  3291. },{"./_stream_transform":24,"core-util-is":26,"inherits":16}],23:[function(require,module,exports){
  3292. (function (process){
  3293. 'use strict';
  3294.  
  3295. module.exports = Readable;
  3296.  
  3297. /*<replacement>*/
  3298. var processNextTick = require('process-nextick-args');
  3299. /*</replacement>*/
  3300.  
  3301.  
  3302. /*<replacement>*/
  3303. var isArray = require('isarray');
  3304. /*</replacement>*/
  3305.  
  3306.  
  3307. /*<replacement>*/
  3308. var Buffer = require('buffer').Buffer;
  3309. /*</replacement>*/
  3310.  
  3311. Readable.ReadableState = ReadableState;
  3312.  
  3313. var EE = require('events');
  3314.  
  3315. /*<replacement>*/
  3316. var EElistenerCount = function(emitter, type) {
  3317. return emitter.listeners(type).length;
  3318. };
  3319. /*</replacement>*/
  3320.  
  3321.  
  3322.  
  3323. /*<replacement>*/
  3324. var Stream;
  3325. (function (){try{
  3326. Stream = require('st' + 'ream');
  3327. }catch(_){}finally{
  3328. if (!Stream)
  3329. Stream = require('events').EventEmitter;
  3330. }}())
  3331. /*</replacement>*/
  3332.  
  3333. var Buffer = require('buffer').Buffer;
  3334.  
  3335. /*<replacement>*/
  3336. var util = require('core-util-is');
  3337. util.inherits = require('inherits');
  3338. /*</replacement>*/
  3339.  
  3340.  
  3341.  
  3342. /*<replacement>*/
  3343. var debugUtil = require('util');
  3344. var debug;
  3345. if (debugUtil && debugUtil.debuglog) {
  3346. debug = debugUtil.debuglog('stream');
  3347. } else {
  3348. debug = function () {};
  3349. }
  3350. /*</replacement>*/
  3351.  
  3352. var StringDecoder;
  3353.  
  3354. util.inherits(Readable, Stream);
  3355.  
  3356. function ReadableState(options, stream) {
  3357. var Duplex = require('./_stream_duplex');
  3358.  
  3359. options = options || {};
  3360.  
  3361. // object stream flag. Used to make read(n) ignore n and to
  3362. // make all the buffer merging and length checks go away
  3363. this.objectMode = !!options.objectMode;
  3364.  
  3365. if (stream instanceof Duplex)
  3366. this.objectMode = this.objectMode || !!options.readableObjectMode;
  3367.  
  3368. // the point at which it stops calling _read() to fill the buffer
  3369. // Note: 0 is a valid value, means "don't call _read preemptively ever"
  3370. var hwm = options.highWaterMark;
  3371. var defaultHwm = this.objectMode ? 16 : 16 * 1024;
  3372. this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
  3373.  
  3374. // cast to ints.
  3375. this.highWaterMark = ~~this.highWaterMark;
  3376.  
  3377. this.buffer = [];
  3378. this.length = 0;
  3379. this.pipes = null;
  3380. this.pipesCount = 0;
  3381. this.flowing = null;
  3382. this.ended = false;
  3383. this.endEmitted = false;
  3384. this.reading = false;
  3385.  
  3386. // a flag to be able to tell if the onwrite cb is called immediately,
  3387. // or on a later tick. We set this to true at first, because any
  3388. // actions that shouldn't happen until "later" should generally also
  3389. // not happen before the first write call.
  3390. this.sync = true;
  3391.  
  3392. // whenever we return null, then we set a flag to say
  3393. // that we're awaiting a 'readable' event emission.
  3394. this.needReadable = false;
  3395. this.emittedReadable = false;
  3396. this.readableListening = false;
  3397.  
  3398. // Crypto is kind of old and crusty. Historically, its default string
  3399. // encoding is 'binary' so we have to make this configurable.
  3400. // Everything else in the universe uses 'utf8', though.
  3401. this.defaultEncoding = options.defaultEncoding || 'utf8';
  3402.  
  3403. // when piping, we only care about 'readable' events that happen
  3404. // after read()ing all the bytes and not getting any pushback.
  3405. this.ranOut = false;
  3406.  
  3407. // the number of writers that are awaiting a drain event in .pipe()s
  3408. this.awaitDrain = 0;
  3409.  
  3410. // if true, a maybeReadMore has been scheduled
  3411. this.readingMore = false;
  3412.  
  3413. this.decoder = null;
  3414. this.encoding = null;
  3415. if (options.encoding) {
  3416. if (!StringDecoder)
  3417. StringDecoder = require('string_decoder/').StringDecoder;
  3418. this.decoder = new StringDecoder(options.encoding);
  3419. this.encoding = options.encoding;
  3420. }
  3421. }
  3422.  
  3423. function Readable(options) {
  3424. var Duplex = require('./_stream_duplex');
  3425.  
  3426. if (!(this instanceof Readable))
  3427. return new Readable(options);
  3428.  
  3429. this._readableState = new ReadableState(options, this);
  3430.  
  3431. // legacy
  3432. this.readable = true;
  3433.  
  3434. if (options && typeof options.read === 'function')
  3435. this._read = options.read;
  3436.  
  3437. Stream.call(this);
  3438. }
  3439.  
  3440. // Manually shove something into the read() buffer.
  3441. // This returns true if the highWaterMark has not been hit yet,
  3442. // similar to how Writable.write() returns true if you should
  3443. // write() some more.
  3444. Readable.prototype.push = function(chunk, encoding) {
  3445. var state = this._readableState;
  3446.  
  3447. if (!state.objectMode && typeof chunk === 'string') {
  3448. encoding = encoding || state.defaultEncoding;
  3449. if (encoding !== state.encoding) {
  3450. chunk = new Buffer(chunk, encoding);
  3451. encoding = '';
  3452. }
  3453. }
  3454.  
  3455. return readableAddChunk(this, state, chunk, encoding, false);
  3456. };
  3457.  
  3458. // Unshift should *always* be something directly out of read()
  3459. Readable.prototype.unshift = function(chunk) {
  3460. var state = this._readableState;
  3461. return readableAddChunk(this, state, chunk, '', true);
  3462. };
  3463.  
  3464. Readable.prototype.isPaused = function() {
  3465. return this._readableState.flowing === false;
  3466. };
  3467.  
  3468. function readableAddChunk(stream, state, chunk, encoding, addToFront) {
  3469. var er = chunkInvalid(state, chunk);
  3470. if (er) {
  3471. stream.emit('error', er);
  3472. } else if (chunk === null) {
  3473. state.reading = false;
  3474. onEofChunk(stream, state);
  3475. } else if (state.objectMode || chunk && chunk.length > 0) {
  3476. if (state.ended && !addToFront) {
  3477. var e = new Error('stream.push() after EOF');
  3478. stream.emit('error', e);
  3479. } else if (state.endEmitted && addToFront) {
  3480. var e = new Error('stream.unshift() after end event');
  3481. stream.emit('error', e);
  3482. } else {
  3483. if (state.decoder && !addToFront && !encoding)
  3484. chunk = state.decoder.write(chunk);
  3485.  
  3486. if (!addToFront)
  3487. state.reading = false;
  3488.  
  3489. // if we want the data now, just emit it.
  3490. if (state.flowing && state.length === 0 && !state.sync) {
  3491. stream.emit('data', chunk);
  3492. stream.read(0);
  3493. } else {
  3494. // update the buffer info.
  3495. state.length += state.objectMode ? 1 : chunk.length;
  3496. if (addToFront)
  3497. state.buffer.unshift(chunk);
  3498. else
  3499. state.buffer.push(chunk);
  3500.  
  3501. if (state.needReadable)
  3502. emitReadable(stream);
  3503. }
  3504.  
  3505. maybeReadMore(stream, state);
  3506. }
  3507. } else if (!addToFront) {
  3508. state.reading = false;
  3509. }
  3510.  
  3511. return needMoreData(state);
  3512. }
  3513.  
  3514.  
  3515. // if it's past the high water mark, we can push in some more.
  3516. // Also, if we have no data yet, we can stand some
  3517. // more bytes. This is to work around cases where hwm=0,
  3518. // such as the repl. Also, if the push() triggered a
  3519. // readable event, and the user called read(largeNumber) such that
  3520. // needReadable was set, then we ought to push more, so that another
  3521. // 'readable' event will be triggered.
  3522. function needMoreData(state) {
  3523. return !state.ended &&
  3524. (state.needReadable ||
  3525. state.length < state.highWaterMark ||
  3526. state.length === 0);
  3527. }
  3528.  
  3529. // backwards compatibility.
  3530. Readable.prototype.setEncoding = function(enc) {
  3531. if (!StringDecoder)
  3532. StringDecoder = require('string_decoder/').StringDecoder;
  3533. this._readableState.decoder = new StringDecoder(enc);
  3534. this._readableState.encoding = enc;
  3535. return this;
  3536. };
  3537.  
  3538. // Don't raise the hwm > 8MB
  3539. var MAX_HWM = 0x800000;
  3540. function computeNewHighWaterMark(n) {
  3541. if (n >= MAX_HWM) {
  3542. n = MAX_HWM;
  3543. } else {
  3544. // Get the next highest power of 2
  3545. n--;
  3546. n |= n >>> 1;
  3547. n |= n >>> 2;
  3548. n |= n >>> 4;
  3549. n |= n >>> 8;
  3550. n |= n >>> 16;
  3551. n++;
  3552. }
  3553. return n;
  3554. }
  3555.  
  3556. function howMuchToRead(n, state) {
  3557. if (state.length === 0 && state.ended)
  3558. return 0;
  3559.  
  3560. if (state.objectMode)
  3561. return n === 0 ? 0 : 1;
  3562.  
  3563. if (n === null || isNaN(n)) {
  3564. // only flow one buffer at a time
  3565. if (state.flowing && state.buffer.length)
  3566. return state.buffer[0].length;
  3567. else
  3568. return state.length;
  3569. }
  3570.  
  3571. if (n <= 0)
  3572. return 0;
  3573.  
  3574. // If we're asking for more than the target buffer level,
  3575. // then raise the water mark. Bump up to the next highest
  3576. // power of 2, to prevent increasing it excessively in tiny
  3577. // amounts.
  3578. if (n > state.highWaterMark)
  3579. state.highWaterMark = computeNewHighWaterMark(n);
  3580.  
  3581. // don't have that much. return null, unless we've ended.
  3582. if (n > state.length) {
  3583. if (!state.ended) {
  3584. state.needReadable = true;
  3585. return 0;
  3586. } else {
  3587. return state.length;
  3588. }
  3589. }
  3590.  
  3591. return n;
  3592. }
  3593.  
  3594. // you can override either this method, or the async _read(n) below.
  3595. Readable.prototype.read = function(n) {
  3596. debug('read', n);
  3597. var state = this._readableState;
  3598. var nOrig = n;
  3599.  
  3600. if (typeof n !== 'number' || n > 0)
  3601. state.emittedReadable = false;
  3602.  
  3603. // if we're doing read(0) to trigger a readable event, but we
  3604. // already have a bunch of data in the buffer, then just trigger
  3605. // the 'readable' event and move on.
  3606. if (n === 0 &&
  3607. state.needReadable &&
  3608. (state.length >= state.highWaterMark || state.ended)) {
  3609. debug('read: emitReadable', state.length, state.ended);
  3610. if (state.length === 0 && state.ended)
  3611. endReadable(this);
  3612. else
  3613. emitReadable(this);
  3614. return null;
  3615. }
  3616.  
  3617. n = howMuchToRead(n, state);
  3618.  
  3619. // if we've ended, and we're now clear, then finish it up.
  3620. if (n === 0 && state.ended) {
  3621. if (state.length === 0)
  3622. endReadable(this);
  3623. return null;
  3624. }
  3625.  
  3626. // All the actual chunk generation logic needs to be
  3627. // *below* the call to _read. The reason is that in certain
  3628. // synthetic stream cases, such as passthrough streams, _read
  3629. // may be a completely synchronous operation which may change
  3630. // the state of the read buffer, providing enough data when
  3631. // before there was *not* enough.
  3632. //
  3633. // So, the steps are:
  3634. // 1. Figure out what the state of things will be after we do
  3635. // a read from the buffer.
  3636. //
  3637. // 2. If that resulting state will trigger a _read, then call _read.
  3638. // Note that this may be asynchronous, or synchronous. Yes, it is
  3639. // deeply ugly to write APIs this way, but that still doesn't mean
  3640. // that the Readable class should behave improperly, as streams are
  3641. // designed to be sync/async agnostic.
  3642. // Take note if the _read call is sync or async (ie, if the read call
  3643. // has returned yet), so that we know whether or not it's safe to emit
  3644. // 'readable' etc.
  3645. //
  3646. // 3. Actually pull the requested chunks out of the buffer and return.
  3647.  
  3648. // if we need a readable event, then we need to do some reading.
  3649. var doRead = state.needReadable;
  3650. debug('need readable', doRead);
  3651.  
  3652. // if we currently have less than the highWaterMark, then also read some
  3653. if (state.length === 0 || state.length - n < state.highWaterMark) {
  3654. doRead = true;
  3655. debug('length less than watermark', doRead);
  3656. }
  3657.  
  3658. // however, if we've ended, then there's no point, and if we're already
  3659. // reading, then it's unnecessary.
  3660. if (state.ended || state.reading) {
  3661. doRead = false;
  3662. debug('reading or ended', doRead);
  3663. }
  3664.  
  3665. if (doRead) {
  3666. debug('do read');
  3667. state.reading = true;
  3668. state.sync = true;
  3669. // if the length is currently zero, then we *need* a readable event.
  3670. if (state.length === 0)
  3671. state.needReadable = true;
  3672. // call internal read method
  3673. this._read(state.highWaterMark);
  3674. state.sync = false;
  3675. }
  3676.  
  3677. // If _read pushed data synchronously, then `reading` will be false,
  3678. // and we need to re-evaluate how much data we can return to the user.
  3679. if (doRead && !state.reading)
  3680. n = howMuchToRead(nOrig, state);
  3681.  
  3682. var ret;
  3683. if (n > 0)
  3684. ret = fromList(n, state);
  3685. else
  3686. ret = null;
  3687.  
  3688. if (ret === null) {
  3689. state.needReadable = true;
  3690. n = 0;
  3691. }
  3692.  
  3693. state.length -= n;
  3694.  
  3695. // If we have nothing in the buffer, then we want to know
  3696. // as soon as we *do* get something into the buffer.
  3697. if (state.length === 0 && !state.ended)
  3698. state.needReadable = true;
  3699.  
  3700. // If we tried to read() past the EOF, then emit end on the next tick.
  3701. if (nOrig !== n && state.ended && state.length === 0)
  3702. endReadable(this);
  3703.  
  3704. if (ret !== null)
  3705. this.emit('data', ret);
  3706.  
  3707. return ret;
  3708. };
  3709.  
  3710. function chunkInvalid(state, chunk) {
  3711. var er = null;
  3712. if (!(Buffer.isBuffer(chunk)) &&
  3713. typeof chunk !== 'string' &&
  3714. chunk !== null &&
  3715. chunk !== undefined &&
  3716. !state.objectMode) {
  3717. er = new TypeError('Invalid non-string/buffer chunk');
  3718. }
  3719. return er;
  3720. }
  3721.  
  3722.  
  3723. function onEofChunk(stream, state) {
  3724. if (state.ended) return;
  3725. if (state.decoder) {
  3726. var chunk = state.decoder.end();
  3727. if (chunk && chunk.length) {
  3728. state.buffer.push(chunk);
  3729. state.length += state.objectMode ? 1 : chunk.length;
  3730. }
  3731. }
  3732. state.ended = true;
  3733.  
  3734. // emit 'readable' now to make sure it gets picked up.
  3735. emitReadable(stream);
  3736. }
  3737.  
  3738. // Don't emit readable right away in sync mode, because this can trigger
  3739. // another read() call => stack overflow. This way, it might trigger
  3740. // a nextTick recursion warning, but that's not so bad.
  3741. function emitReadable(stream) {
  3742. var state = stream._readableState;
  3743. state.needReadable = false;
  3744. if (!state.emittedReadable) {
  3745. debug('emitReadable', state.flowing);
  3746. state.emittedReadable = true;
  3747. if (state.sync)
  3748. processNextTick(emitReadable_, stream);
  3749. else
  3750. emitReadable_(stream);
  3751. }
  3752. }
  3753.  
  3754. function emitReadable_(stream) {
  3755. debug('emit readable');
  3756. stream.emit('readable');
  3757. flow(stream);
  3758. }
  3759.  
  3760.  
  3761. // at this point, the user has presumably seen the 'readable' event,
  3762. // and called read() to consume some data. that may have triggered
  3763. // in turn another _read(n) call, in which case reading = true if
  3764. // it's in progress.
  3765. // However, if we're not ended, or reading, and the length < hwm,
  3766. // then go ahead and try to read some more preemptively.
  3767. function maybeReadMore(stream, state) {
  3768. if (!state.readingMore) {
  3769. state.readingMore = true;
  3770. processNextTick(maybeReadMore_, stream, state);
  3771. }
  3772. }
  3773.  
  3774. function maybeReadMore_(stream, state) {
  3775. var len = state.length;
  3776. while (!state.reading && !state.flowing && !state.ended &&
  3777. state.length < state.highWaterMark) {
  3778. debug('maybeReadMore read 0');
  3779. stream.read(0);
  3780. if (len === state.length)
  3781. // didn't get any data, stop spinning.
  3782. break;
  3783. else
  3784. len = state.length;
  3785. }
  3786. state.readingMore = false;
  3787. }
  3788.  
  3789. // abstract method. to be overridden in specific implementation classes.
  3790. // call cb(er, data) where data is <= n in length.
  3791. // for virtual (non-string, non-buffer) streams, "length" is somewhat
  3792. // arbitrary, and perhaps not very meaningful.
  3793. Readable.prototype._read = function(n) {
  3794. this.emit('error', new Error('not implemented'));
  3795. };
  3796.  
  3797. Readable.prototype.pipe = function(dest, pipeOpts) {
  3798. var src = this;
  3799. var state = this._readableState;
  3800.  
  3801. switch (state.pipesCount) {
  3802. case 0:
  3803. state.pipes = dest;
  3804. break;
  3805. case 1:
  3806. state.pipes = [state.pipes, dest];
  3807. break;
  3808. default:
  3809. state.pipes.push(dest);
  3810. break;
  3811. }
  3812. state.pipesCount += 1;
  3813. debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
  3814.  
  3815. var doEnd = (!pipeOpts || pipeOpts.end !== false) &&
  3816. dest !== process.stdout &&
  3817. dest !== process.stderr;
  3818.  
  3819. var endFn = doEnd ? onend : cleanup;
  3820. if (state.endEmitted)
  3821. processNextTick(endFn);
  3822. else
  3823. src.once('end', endFn);
  3824.  
  3825. dest.on('unpipe', onunpipe);
  3826. function onunpipe(readable) {
  3827. debug('onunpipe');
  3828. if (readable === src) {
  3829. cleanup();
  3830. }
  3831. }
  3832.  
  3833. function onend() {
  3834. debug('onend');
  3835. dest.end();
  3836. }
  3837.  
  3838. // when the dest drains, it reduces the awaitDrain counter
  3839. // on the source. This would be more elegant with a .once()
  3840. // handler in flow(), but adding and removing repeatedly is
  3841. // too slow.
  3842. var ondrain = pipeOnDrain(src);
  3843. dest.on('drain', ondrain);
  3844.  
  3845. var cleanedUp = false;
  3846. function cleanup() {
  3847. debug('cleanup');
  3848. // cleanup event handlers once the pipe is broken
  3849. dest.removeListener('close', onclose);
  3850. dest.removeListener('finish', onfinish);
  3851. dest.removeListener('drain', ondrain);
  3852. dest.removeListener('error', onerror);
  3853. dest.removeListener('unpipe', onunpipe);
  3854. src.removeListener('end', onend);
  3855. src.removeListener('end', cleanup);
  3856. src.removeListener('data', ondata);
  3857.  
  3858. cleanedUp = true;
  3859.  
  3860. // if the reader is waiting for a drain event from this
  3861. // specific writer, then it would cause it to never start
  3862. // flowing again.
  3863. // So, if this is awaiting a drain, then we just call it now.
  3864. // If we don't know, then assume that we are waiting for one.
  3865. if (state.awaitDrain &&
  3866. (!dest._writableState || dest._writableState.needDrain))
  3867. ondrain();
  3868. }
  3869.  
  3870. src.on('data', ondata);
  3871. function ondata(chunk) {
  3872. debug('ondata');
  3873. var ret = dest.write(chunk);
  3874. if (false === ret) {
  3875. // If the user unpiped during `dest.write()`, it is possible
  3876. // to get stuck in a permanently paused state if that write
  3877. // also returned false.
  3878. if (state.pipesCount === 1 &&
  3879. state.pipes[0] === dest &&
  3880. src.listenerCount('data') === 1 &&
  3881. !cleanedUp) {
  3882. debug('false write response, pause', src._readableState.awaitDrain);
  3883. src._readableState.awaitDrain++;
  3884. }
  3885. src.pause();
  3886. }
  3887. }
  3888.  
  3889. // if the dest has an error, then stop piping into it.
  3890. // however, don't suppress the throwing behavior for this.
  3891. function onerror(er) {
  3892. debug('onerror', er);
  3893. unpipe();
  3894. dest.removeListener('error', onerror);
  3895. if (EElistenerCount(dest, 'error') === 0)
  3896. dest.emit('error', er);
  3897. }
  3898. // This is a brutally ugly hack to make sure that our error handler
  3899. // is attached before any userland ones. NEVER DO THIS.
  3900. if (!dest._events || !dest._events.error)
  3901. dest.on('error', onerror);
  3902. else if (isArray(dest._events.error))
  3903. dest._events.error.unshift(onerror);
  3904. else
  3905. dest._events.error = [onerror, dest._events.error];
  3906.  
  3907.  
  3908. // Both close and finish should trigger unpipe, but only once.
  3909. function onclose() {
  3910. dest.removeListener('finish', onfinish);
  3911. unpipe();
  3912. }
  3913. dest.once('close', onclose);
  3914. function onfinish() {
  3915. debug('onfinish');
  3916. dest.removeListener('close', onclose);
  3917. unpipe();
  3918. }
  3919. dest.once('finish', onfinish);
  3920.  
  3921. function unpipe() {
  3922. debug('unpipe');
  3923. src.unpipe(dest);
  3924. }
  3925.  
  3926. // tell the dest that it's being piped to
  3927. dest.emit('pipe', src);
  3928.  
  3929. // start the flow if it hasn't been started already.
  3930. if (!state.flowing) {
  3931. debug('pipe resume');
  3932. src.resume();
  3933. }
  3934.  
  3935. return dest;
  3936. };
  3937.  
  3938. function pipeOnDrain(src) {
  3939. return function() {
  3940. var state = src._readableState;
  3941. debug('pipeOnDrain', state.awaitDrain);
  3942. if (state.awaitDrain)
  3943. state.awaitDrain--;
  3944. if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) {
  3945. state.flowing = true;
  3946. flow(src);
  3947. }
  3948. };
  3949. }
  3950.  
  3951.  
  3952. Readable.prototype.unpipe = function(dest) {
  3953. var state = this._readableState;
  3954.  
  3955. // if we're not piping anywhere, then do nothing.
  3956. if (state.pipesCount === 0)
  3957. return this;
  3958.  
  3959. // just one destination. most common case.
  3960. if (state.pipesCount === 1) {
  3961. // passed in one, but it's not the right one.
  3962. if (dest && dest !== state.pipes)
  3963. return this;
  3964.  
  3965. if (!dest)
  3966. dest = state.pipes;
  3967.  
  3968. // got a match.
  3969. state.pipes = null;
  3970. state.pipesCount = 0;
  3971. state.flowing = false;
  3972. if (dest)
  3973. dest.emit('unpipe', this);
  3974. return this;
  3975. }
  3976.  
  3977. // slow case. multiple pipe destinations.
  3978.  
  3979. if (!dest) {
  3980. // remove all.
  3981. var dests = state.pipes;
  3982. var len = state.pipesCount;
  3983. state.pipes = null;
  3984. state.pipesCount = 0;
  3985. state.flowing = false;
  3986.  
  3987. for (var i = 0; i < len; i++)
  3988. dests[i].emit('unpipe', this);
  3989. return this;
  3990. }
  3991.  
  3992. // try to find the right one.
  3993. var i = indexOf(state.pipes, dest);
  3994. if (i === -1)
  3995. return this;
  3996.  
  3997. state.pipes.splice(i, 1);
  3998. state.pipesCount -= 1;
  3999. if (state.pipesCount === 1)
  4000. state.pipes = state.pipes[0];
  4001.  
  4002. dest.emit('unpipe', this);
  4003.  
  4004. return this;
  4005. };
  4006.  
  4007. // set up data events if they are asked for
  4008. // Ensure readable listeners eventually get something
  4009. Readable.prototype.on = function(ev, fn) {
  4010. var res = Stream.prototype.on.call(this, ev, fn);
  4011.  
  4012. // If listening to data, and it has not explicitly been paused,
  4013. // then call resume to start the flow of data on the next tick.
  4014. if (ev === 'data' && false !== this._readableState.flowing) {
  4015. this.resume();
  4016. }
  4017.  
  4018. if (ev === 'readable' && this.readable) {
  4019. var state = this._readableState;
  4020. if (!state.readableListening) {
  4021. state.readableListening = true;
  4022. state.emittedReadable = false;
  4023. state.needReadable = true;
  4024. if (!state.reading) {
  4025. processNextTick(nReadingNextTick, this);
  4026. } else if (state.length) {
  4027. emitReadable(this, state);
  4028. }
  4029. }
  4030. }
  4031.  
  4032. return res;
  4033. };
  4034. Readable.prototype.addListener = Readable.prototype.on;
  4035.  
  4036. function nReadingNextTick(self) {
  4037. debug('readable nexttick read 0');
  4038. self.read(0);
  4039. }
  4040.  
  4041. // pause() and resume() are remnants of the legacy readable stream API
  4042. // If the user uses them, then switch into old mode.
  4043. Readable.prototype.resume = function() {
  4044. var state = this._readableState;
  4045. if (!state.flowing) {
  4046. debug('resume');
  4047. state.flowing = true;
  4048. resume(this, state);
  4049. }
  4050. return this;
  4051. };
  4052.  
  4053. function resume(stream, state) {
  4054. if (!state.resumeScheduled) {
  4055. state.resumeScheduled = true;
  4056. processNextTick(resume_, stream, state);
  4057. }
  4058. }
  4059.  
  4060. function resume_(stream, state) {
  4061. if (!state.reading) {
  4062. debug('resume read 0');
  4063. stream.read(0);
  4064. }
  4065.  
  4066. state.resumeScheduled = false;
  4067. stream.emit('resume');
  4068. flow(stream);
  4069. if (state.flowing && !state.reading)
  4070. stream.read(0);
  4071. }
  4072.  
  4073. Readable.prototype.pause = function() {
  4074. debug('call pause flowing=%j', this._readableState.flowing);
  4075. if (false !== this._readableState.flowing) {
  4076. debug('pause');
  4077. this._readableState.flowing = false;
  4078. this.emit('pause');
  4079. }
  4080. return this;
  4081. };
  4082.  
  4083. function flow(stream) {
  4084. var state = stream._readableState;
  4085. debug('flow', state.flowing);
  4086. if (state.flowing) {
  4087. do {
  4088. var chunk = stream.read();
  4089. } while (null !== chunk && state.flowing);
  4090. }
  4091. }
  4092.  
  4093. // wrap an old-style stream as the async data source.
  4094. // This is *not* part of the readable stream interface.
  4095. // It is an ugly unfortunate mess of history.
  4096. Readable.prototype.wrap = function(stream) {
  4097. var state = this._readableState;
  4098. var paused = false;
  4099.  
  4100. var self = this;
  4101. stream.on('end', function() {
  4102. debug('wrapped end');
  4103. if (state.decoder && !state.ended) {
  4104. var chunk = state.decoder.end();
  4105. if (chunk && chunk.length)
  4106. self.push(chunk);
  4107. }
  4108.  
  4109. self.push(null);
  4110. });
  4111.  
  4112. stream.on('data', function(chunk) {
  4113. debug('wrapped data');
  4114. if (state.decoder)
  4115. chunk = state.decoder.write(chunk);
  4116.  
  4117. // don't skip over falsy values in objectMode
  4118. if (state.objectMode && (chunk === null || chunk === undefined))
  4119. return;
  4120. else if (!state.objectMode && (!chunk || !chunk.length))
  4121. return;
  4122.  
  4123. var ret = self.push(chunk);
  4124. if (!ret) {
  4125. paused = true;
  4126. stream.pause();
  4127. }
  4128. });
  4129.  
  4130. // proxy all the other methods.
  4131. // important when wrapping filters and duplexes.
  4132. for (var i in stream) {
  4133. if (this[i] === undefined && typeof stream[i] === 'function') {
  4134. this[i] = function(method) { return function() {
  4135. return stream[method].apply(stream, arguments);
  4136. }; }(i);
  4137. }
  4138. }
  4139.  
  4140. // proxy certain important events.
  4141. var events = ['error', 'close', 'destroy', 'pause', 'resume'];
  4142. forEach(events, function(ev) {
  4143. stream.on(ev, self.emit.bind(self, ev));
  4144. });
  4145.  
  4146. // when we try to consume some more bytes, simply unpause the
  4147. // underlying stream.
  4148. self._read = function(n) {
  4149. debug('wrapped _read', n);
  4150. if (paused) {
  4151. paused = false;
  4152. stream.resume();
  4153. }
  4154. };
  4155.  
  4156. return self;
  4157. };
  4158.  
  4159.  
  4160. // exposed for testing purposes only.
  4161. Readable._fromList = fromList;
  4162.  
  4163. // Pluck off n bytes from an array of buffers.
  4164. // Length is the combined lengths of all the buffers in the list.
  4165. function fromList(n, state) {
  4166. var list = state.buffer;
  4167. var length = state.length;
  4168. var stringMode = !!state.decoder;
  4169. var objectMode = !!state.objectMode;
  4170. var ret;
  4171.  
  4172. // nothing in the list, definitely empty.
  4173. if (list.length === 0)
  4174. return null;
  4175.  
  4176. if (length === 0)
  4177. ret = null;
  4178. else if (objectMode)
  4179. ret = list.shift();
  4180. else if (!n || n >= length) {
  4181. // read it all, truncate the array.
  4182. if (stringMode)
  4183. ret = list.join('');
  4184. else if (list.length === 1)
  4185. ret = list[0];
  4186. else
  4187. ret = Buffer.concat(list, length);
  4188. list.length = 0;
  4189. } else {
  4190. // read just some of it.
  4191. if (n < list[0].length) {
  4192. // just take a part of the first list item.
  4193. // slice is the same for buffers and strings.
  4194. var buf = list[0];
  4195. ret = buf.slice(0, n);
  4196. list[0] = buf.slice(n);
  4197. } else if (n === list[0].length) {
  4198. // first list is a perfect match
  4199. ret = list.shift();
  4200. } else {
  4201. // complex case.
  4202. // we have enough to cover it, but it spans past the first buffer.
  4203. if (stringMode)
  4204. ret = '';
  4205. else
  4206. ret = new Buffer(n);
  4207.  
  4208. var c = 0;
  4209. for (var i = 0, l = list.length; i < l && c < n; i++) {
  4210. var buf = list[0];
  4211. var cpy = Math.min(n - c, buf.length);
  4212.  
  4213. if (stringMode)
  4214. ret += buf.slice(0, cpy);
  4215. else
  4216. buf.copy(ret, c, 0, cpy);
  4217.  
  4218. if (cpy < buf.length)
  4219. list[0] = buf.slice(cpy);
  4220. else
  4221. list.shift();
  4222.  
  4223. c += cpy;
  4224. }
  4225. }
  4226. }
  4227.  
  4228. return ret;
  4229. }
  4230.  
  4231. function endReadable(stream) {
  4232. var state = stream._readableState;
  4233.  
  4234. // If we get here before consuming all the bytes, then that is a
  4235. // bug in node. Should never happen.
  4236. if (state.length > 0)
  4237. throw new Error('endReadable called on non-empty stream');
  4238.  
  4239. if (!state.endEmitted) {
  4240. state.ended = true;
  4241. processNextTick(endReadableNT, state, stream);
  4242. }
  4243. }
  4244.  
  4245. function endReadableNT(state, stream) {
  4246. // Check that we didn't get one last unshift.
  4247. if (!state.endEmitted && state.length === 0) {
  4248. state.endEmitted = true;
  4249. stream.readable = false;
  4250. stream.emit('end');
  4251. }
  4252. }
  4253.  
  4254. function forEach (xs, f) {
  4255. for (var i = 0, l = xs.length; i < l; i++) {
  4256. f(xs[i], i);
  4257. }
  4258. }
  4259.  
  4260. function indexOf (xs, x) {
  4261. for (var i = 0, l = xs.length; i < l; i++) {
  4262. if (xs[i] === x) return i;
  4263. }
  4264. return -1;
  4265. }
  4266.  
  4267. }).call(this,require('_process'))
  4268. },{"./_stream_duplex":21,"_process":19,"buffer":"buffer","core-util-is":26,"events":15,"inherits":16,"isarray":18,"process-nextick-args":27,"string_decoder/":34,"util":11}],24:[function(require,module,exports){
  4269. // a transform stream is a readable/writable stream where you do
  4270. // something with the data. Sometimes it's called a "filter",
  4271. // but that's not a great name for it, since that implies a thing where
  4272. // some bits pass through, and others are simply ignored. (That would
  4273. // be a valid example of a transform, of course.)
  4274. //
  4275. // While the output is causally related to the input, it's not a
  4276. // necessarily symmetric or synchronous transformation. For example,
  4277. // a zlib stream might take multiple plain-text writes(), and then
  4278. // emit a single compressed chunk some time in the future.
  4279. //
  4280. // Here's how this works:
  4281. //
  4282. // The Transform stream has all the aspects of the readable and writable
  4283. // stream classes. When you write(chunk), that calls _write(chunk,cb)
  4284. // internally, and returns false if there's a lot of pending writes
  4285. // buffered up. When you call read(), that calls _read(n) until
  4286. // there's enough pending readable data buffered up.
  4287. //
  4288. // In a transform stream, the written data is placed in a buffer. When
  4289. // _read(n) is called, it transforms the queued up data, calling the
  4290. // buffered _write cb's as it consumes chunks. If consuming a single
  4291. // written chunk would result in multiple output chunks, then the first
  4292. // outputted bit calls the readcb, and subsequent chunks just go into
  4293. // the read buffer, and will cause it to emit 'readable' if necessary.
  4294. //
  4295. // This way, back-pressure is actually determined by the reading side,
  4296. // since _read has to be called to start processing a new chunk. However,
  4297. // a pathological inflate type of transform can cause excessive buffering
  4298. // here. For example, imagine a stream where every byte of input is
  4299. // interpreted as an integer from 0-255, and then results in that many
  4300. // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in
  4301. // 1kb of data being output. In this case, you could write a very small
  4302. // amount of input, and end up with a very large amount of output. In
  4303. // such a pathological inflating mechanism, there'd be no way to tell
  4304. // the system to stop doing the transform. A single 4MB write could
  4305. // cause the system to run out of memory.
  4306. //
  4307. // However, even in such a pathological case, only a single written chunk
  4308. // would be consumed, and then the rest would wait (un-transformed) until
  4309. // the results of the previous transformed chunk were consumed.
  4310.  
  4311. 'use strict';
  4312.  
  4313. module.exports = Transform;
  4314.  
  4315. var Duplex = require('./_stream_duplex');
  4316.  
  4317. /*<replacement>*/
  4318. var util = require('core-util-is');
  4319. util.inherits = require('inherits');
  4320. /*</replacement>*/
  4321.  
  4322. util.inherits(Transform, Duplex);
  4323.  
  4324.  
  4325. function TransformState(stream) {
  4326. this.afterTransform = function(er, data) {
  4327. return afterTransform(stream, er, data);
  4328. };
  4329.  
  4330. this.needTransform = false;
  4331. this.transforming = false;
  4332. this.writecb = null;
  4333. this.writechunk = null;
  4334. }
  4335.  
  4336. function afterTransform(stream, er, data) {
  4337. var ts = stream._transformState;
  4338. ts.transforming = false;
  4339.  
  4340. var cb = ts.writecb;
  4341.  
  4342. if (!cb)
  4343. return stream.emit('error', new Error('no writecb in Transform class'));
  4344.  
  4345. ts.writechunk = null;
  4346. ts.writecb = null;
  4347.  
  4348. if (data !== null && data !== undefined)
  4349. stream.push(data);
  4350.  
  4351. if (cb)
  4352. cb(er);
  4353.  
  4354. var rs = stream._readableState;
  4355. rs.reading = false;
  4356. if (rs.needReadable || rs.length < rs.highWaterMark) {
  4357. stream._read(rs.highWaterMark);
  4358. }
  4359. }
  4360.  
  4361.  
  4362. function Transform(options) {
  4363. if (!(this instanceof Transform))
  4364. return new Transform(options);
  4365.  
  4366. Duplex.call(this, options);
  4367.  
  4368. this._transformState = new TransformState(this);
  4369.  
  4370. // when the writable side finishes, then flush out anything remaining.
  4371. var stream = this;
  4372.  
  4373. // start out asking for a readable event once data is transformed.
  4374. this._readableState.needReadable = true;
  4375.  
  4376. // we have implemented the _read method, and done the other things
  4377. // that Readable wants before the first _read call, so unset the
  4378. // sync guard flag.
  4379. this._readableState.sync = false;
  4380.  
  4381. if (options) {
  4382. if (typeof options.transform === 'function')
  4383. this._transform = options.transform;
  4384.  
  4385. if (typeof options.flush === 'function')
  4386. this._flush = options.flush;
  4387. }
  4388.  
  4389. this.once('prefinish', function() {
  4390. if (typeof this._flush === 'function')
  4391. this._flush(function(er) {
  4392. done(stream, er);
  4393. });
  4394. else
  4395. done(stream);
  4396. });
  4397. }
  4398.  
  4399. Transform.prototype.push = function(chunk, encoding) {
  4400. this._transformState.needTransform = false;
  4401. return Duplex.prototype.push.call(this, chunk, encoding);
  4402. };
  4403.  
  4404. // This is the part where you do stuff!
  4405. // override this function in implementation classes.
  4406. // 'chunk' is an input chunk.
  4407. //
  4408. // Call `push(newChunk)` to pass along transformed output
  4409. // to the readable side. You may call 'push' zero or more times.
  4410. //
  4411. // Call `cb(err)` when you are done with this chunk. If you pass
  4412. // an error, then that'll put the hurt on the whole operation. If you
  4413. // never call cb(), then you'll never get another chunk.
  4414. Transform.prototype._transform = function(chunk, encoding, cb) {
  4415. throw new Error('not implemented');
  4416. };
  4417.  
  4418. Transform.prototype._write = function(chunk, encoding, cb) {
  4419. var ts = this._transformState;
  4420. ts.writecb = cb;
  4421. ts.writechunk = chunk;
  4422. ts.writeencoding = encoding;
  4423. if (!ts.transforming) {
  4424. var rs = this._readableState;
  4425. if (ts.needTransform ||
  4426. rs.needReadable ||
  4427. rs.length < rs.highWaterMark)
  4428. this._read(rs.highWaterMark);
  4429. }
  4430. };
  4431.  
  4432. // Doesn't matter what the args are here.
  4433. // _transform does all the work.
  4434. // That we got here means that the readable side wants more data.
  4435. Transform.prototype._read = function(n) {
  4436. var ts = this._transformState;
  4437.  
  4438. if (ts.writechunk !== null && ts.writecb && !ts.transforming) {
  4439. ts.transforming = true;
  4440. this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
  4441. } else {
  4442. // mark that we need a transform, so that any data that comes in
  4443. // will get processed, now that we've asked for it.
  4444. ts.needTransform = true;
  4445. }
  4446. };
  4447.  
  4448.  
  4449. function done(stream, er) {
  4450. if (er)
  4451. return stream.emit('error', er);
  4452.  
  4453. // if there's nothing in the write buffer, then that means
  4454. // that nothing more will ever be provided
  4455. var ws = stream._writableState;
  4456. var ts = stream._transformState;
  4457.  
  4458. if (ws.length)
  4459. throw new Error('calling transform done when ws.length != 0');
  4460.  
  4461. if (ts.transforming)
  4462. throw new Error('calling transform done when still transforming');
  4463.  
  4464. return stream.push(null);
  4465. }
  4466.  
  4467. },{"./_stream_duplex":21,"core-util-is":26,"inherits":16}],25:[function(require,module,exports){
  4468. // A bit simpler than readable streams.
  4469. // Implement an async ._write(chunk, encoding, cb), and it'll handle all
  4470. // the drain event emission and buffering.
  4471.  
  4472. 'use strict';
  4473.  
  4474. module.exports = Writable;
  4475.  
  4476. /*<replacement>*/
  4477. var processNextTick = require('process-nextick-args');
  4478. /*</replacement>*/
  4479.  
  4480.  
  4481. /*<replacement>*/
  4482. var Buffer = require('buffer').Buffer;
  4483. /*</replacement>*/
  4484.  
  4485. Writable.WritableState = WritableState;
  4486.  
  4487.  
  4488. /*<replacement>*/
  4489. var util = require('core-util-is');
  4490. util.inherits = require('inherits');
  4491. /*</replacement>*/
  4492.  
  4493.  
  4494. /*<replacement>*/
  4495. var internalUtil = {
  4496. deprecate: require('util-deprecate')
  4497. };
  4498. /*</replacement>*/
  4499.  
  4500.  
  4501.  
  4502. /*<replacement>*/
  4503. var Stream;
  4504. (function (){try{
  4505. Stream = require('st' + 'ream');
  4506. }catch(_){}finally{
  4507. if (!Stream)
  4508. Stream = require('events').EventEmitter;
  4509. }}())
  4510. /*</replacement>*/
  4511.  
  4512. var Buffer = require('buffer').Buffer;
  4513.  
  4514. util.inherits(Writable, Stream);
  4515.  
  4516. function nop() {}
  4517.  
  4518. function WriteReq(chunk, encoding, cb) {
  4519. this.chunk = chunk;
  4520. this.encoding = encoding;
  4521. this.callback = cb;
  4522. this.next = null;
  4523. }
  4524.  
  4525. function WritableState(options, stream) {
  4526. var Duplex = require('./_stream_duplex');
  4527.  
  4528. options = options || {};
  4529.  
  4530. // object stream flag to indicate whether or not this stream
  4531. // contains buffers or objects.
  4532. this.objectMode = !!options.objectMode;
  4533.  
  4534. if (stream instanceof Duplex)
  4535. this.objectMode = this.objectMode || !!options.writableObjectMode;
  4536.  
  4537. // the point at which write() starts returning false
  4538. // Note: 0 is a valid value, means that we always return false if
  4539. // the entire buffer is not flushed immediately on write()
  4540. var hwm = options.highWaterMark;
  4541. var defaultHwm = this.objectMode ? 16 : 16 * 1024;
  4542. this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
  4543.  
  4544. // cast to ints.
  4545. this.highWaterMark = ~~this.highWaterMark;
  4546.  
  4547. this.needDrain = false;
  4548. // at the start of calling end()
  4549. this.ending = false;
  4550. // when end() has been called, and returned
  4551. this.ended = false;
  4552. // when 'finish' is emitted
  4553. this.finished = false;
  4554.  
  4555. // should we decode strings into buffers before passing to _write?
  4556. // this is here so that some node-core streams can optimize string
  4557. // handling at a lower level.
  4558. var noDecode = options.decodeStrings === false;
  4559. this.decodeStrings = !noDecode;
  4560.  
  4561. // Crypto is kind of old and crusty. Historically, its default string
  4562. // encoding is 'binary' so we have to make this configurable.
  4563. // Everything else in the universe uses 'utf8', though.
  4564. this.defaultEncoding = options.defaultEncoding || 'utf8';
  4565.  
  4566. // not an actual buffer we keep track of, but a measurement
  4567. // of how much we're waiting to get pushed to some underlying
  4568. // socket or file.
  4569. this.length = 0;
  4570.  
  4571. // a flag to see when we're in the middle of a write.
  4572. this.writing = false;
  4573.  
  4574. // when true all writes will be buffered until .uncork() call
  4575. this.corked = 0;
  4576.  
  4577. // a flag to be able to tell if the onwrite cb is called immediately,
  4578. // or on a later tick. We set this to true at first, because any
  4579. // actions that shouldn't happen until "later" should generally also
  4580. // not happen before the first write call.
  4581. this.sync = true;
  4582.  
  4583. // a flag to know if we're processing previously buffered items, which
  4584. // may call the _write() callback in the same tick, so that we don't
  4585. // end up in an overlapped onwrite situation.
  4586. this.bufferProcessing = false;
  4587.  
  4588. // the callback that's passed to _write(chunk,cb)
  4589. this.onwrite = function(er) {
  4590. onwrite(stream, er);
  4591. };
  4592.  
  4593. // the callback that the user supplies to write(chunk,encoding,cb)
  4594. this.writecb = null;
  4595.  
  4596. // the amount that is being written when _write is called.
  4597. this.writelen = 0;
  4598.  
  4599. this.bufferedRequest = null;
  4600. this.lastBufferedRequest = null;
  4601.  
  4602. // number of pending user-supplied write callbacks
  4603. // this must be 0 before 'finish' can be emitted
  4604. this.pendingcb = 0;
  4605.  
  4606. // emit prefinish if the only thing we're waiting for is _write cbs
  4607. // This is relevant for synchronous Transform streams
  4608. this.prefinished = false;
  4609.  
  4610. // True if the error was already emitted and should not be thrown again
  4611. this.errorEmitted = false;
  4612. }
  4613.  
  4614. WritableState.prototype.getBuffer = function writableStateGetBuffer() {
  4615. var current = this.bufferedRequest;
  4616. var out = [];
  4617. while (current) {
  4618. out.push(current);
  4619. current = current.next;
  4620. }
  4621. return out;
  4622. };
  4623.  
  4624. (function (){try {
  4625. Object.defineProperty(WritableState.prototype, 'buffer', {
  4626. get: internalUtil.deprecate(function() {
  4627. return this.getBuffer();
  4628. }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' +
  4629. 'instead.')
  4630. });
  4631. }catch(_){}}());
  4632.  
  4633.  
  4634. function Writable(options) {
  4635. var Duplex = require('./_stream_duplex');
  4636.  
  4637. // Writable ctor is applied to Duplexes, though they're not
  4638. // instanceof Writable, they're instanceof Readable.
  4639. if (!(this instanceof Writable) && !(this instanceof Duplex))
  4640. return new Writable(options);
  4641.  
  4642. this._writableState = new WritableState(options, this);
  4643.  
  4644. // legacy.
  4645. this.writable = true;
  4646.  
  4647. if (options) {
  4648. if (typeof options.write === 'function')
  4649. this._write = options.write;
  4650.  
  4651. if (typeof options.writev === 'function')
  4652. this._writev = options.writev;
  4653. }
  4654.  
  4655. Stream.call(this);
  4656. }
  4657.  
  4658. // Otherwise people can pipe Writable streams, which is just wrong.
  4659. Writable.prototype.pipe = function() {
  4660. this.emit('error', new Error('Cannot pipe. Not readable.'));
  4661. };
  4662.  
  4663.  
  4664. function writeAfterEnd(stream, cb) {
  4665. var er = new Error('write after end');
  4666. // TODO: defer error events consistently everywhere, not just the cb
  4667. stream.emit('error', er);
  4668. processNextTick(cb, er);
  4669. }
  4670.  
  4671. // If we get something that is not a buffer, string, null, or undefined,
  4672. // and we're not in objectMode, then that's an error.
  4673. // Otherwise stream chunks are all considered to be of length=1, and the
  4674. // watermarks determine how many objects to keep in the buffer, rather than
  4675. // how many bytes or characters.
  4676. function validChunk(stream, state, chunk, cb) {
  4677. var valid = true;
  4678.  
  4679. if (!(Buffer.isBuffer(chunk)) &&
  4680. typeof chunk !== 'string' &&
  4681. chunk !== null &&
  4682. chunk !== undefined &&
  4683. !state.objectMode) {
  4684. var er = new TypeError('Invalid non-string/buffer chunk');
  4685. stream.emit('error', er);
  4686. processNextTick(cb, er);
  4687. valid = false;
  4688. }
  4689. return valid;
  4690. }
  4691.  
  4692. Writable.prototype.write = function(chunk, encoding, cb) {
  4693. var state = this._writableState;
  4694. var ret = false;
  4695.  
  4696. if (typeof encoding === 'function') {
  4697. cb = encoding;
  4698. encoding = null;
  4699. }
  4700.  
  4701. if (Buffer.isBuffer(chunk))
  4702. encoding = 'buffer';
  4703. else if (!encoding)
  4704. encoding = state.defaultEncoding;
  4705.  
  4706. if (typeof cb !== 'function')
  4707. cb = nop;
  4708.  
  4709. if (state.ended)
  4710. writeAfterEnd(this, cb);
  4711. else if (validChunk(this, state, chunk, cb)) {
  4712. state.pendingcb++;
  4713. ret = writeOrBuffer(this, state, chunk, encoding, cb);
  4714. }
  4715.  
  4716. return ret;
  4717. };
  4718.  
  4719. Writable.prototype.cork = function() {
  4720. var state = this._writableState;
  4721.  
  4722. state.corked++;
  4723. };
  4724.  
  4725. Writable.prototype.uncork = function() {
  4726. var state = this._writableState;
  4727.  
  4728. if (state.corked) {
  4729. state.corked--;
  4730.  
  4731. if (!state.writing &&
  4732. !state.corked &&
  4733. !state.finished &&
  4734. !state.bufferProcessing &&
  4735. state.bufferedRequest)
  4736. clearBuffer(this, state);
  4737. }
  4738. };
  4739.  
  4740. Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
  4741. // node::ParseEncoding() requires lower case.
  4742. if (typeof encoding === 'string')
  4743. encoding = encoding.toLowerCase();
  4744. if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64',
  4745. 'ucs2', 'ucs-2','utf16le', 'utf-16le', 'raw']
  4746. .indexOf((encoding + '').toLowerCase()) > -1))
  4747. throw new TypeError('Unknown encoding: ' + encoding);
  4748. this._writableState.defaultEncoding = encoding;
  4749. };
  4750.  
  4751. function decodeChunk(state, chunk, encoding) {
  4752. if (!state.objectMode &&
  4753. state.decodeStrings !== false &&
  4754. typeof chunk === 'string') {
  4755. chunk = new Buffer(chunk, encoding);
  4756. }
  4757. return chunk;
  4758. }
  4759.  
  4760. // if we're already writing something, then just put this
  4761. // in the queue, and wait our turn. Otherwise, call _write
  4762. // If we return false, then we need a drain event, so set that flag.
  4763. function writeOrBuffer(stream, state, chunk, encoding, cb) {
  4764. chunk = decodeChunk(state, chunk, encoding);
  4765.  
  4766. if (Buffer.isBuffer(chunk))
  4767. encoding = 'buffer';
  4768. var len = state.objectMode ? 1 : chunk.length;
  4769.  
  4770. state.length += len;
  4771.  
  4772. var ret = state.length < state.highWaterMark;
  4773. // we must ensure that previous needDrain will not be reset to false.
  4774. if (!ret)
  4775. state.needDrain = true;
  4776.  
  4777. if (state.writing || state.corked) {
  4778. var last = state.lastBufferedRequest;
  4779. state.lastBufferedRequest = new WriteReq(chunk, encoding, cb);
  4780. if (last) {
  4781. last.next = state.lastBufferedRequest;
  4782. } else {
  4783. state.bufferedRequest = state.lastBufferedRequest;
  4784. }
  4785. } else {
  4786. doWrite(stream, state, false, len, chunk, encoding, cb);
  4787. }
  4788.  
  4789. return ret;
  4790. }
  4791.  
  4792. function doWrite(stream, state, writev, len, chunk, encoding, cb) {
  4793. state.writelen = len;
  4794. state.writecb = cb;
  4795. state.writing = true;
  4796. state.sync = true;
  4797. if (writev)
  4798. stream._writev(chunk, state.onwrite);
  4799. else
  4800. stream._write(chunk, encoding, state.onwrite);
  4801. state.sync = false;
  4802. }
  4803.  
  4804. function onwriteError(stream, state, sync, er, cb) {
  4805. --state.pendingcb;
  4806. if (sync)
  4807. processNextTick(cb, er);
  4808. else
  4809. cb(er);
  4810.  
  4811. stream._writableState.errorEmitted = true;
  4812. stream.emit('error', er);
  4813. }
  4814.  
  4815. function onwriteStateUpdate(state) {
  4816. state.writing = false;
  4817. state.writecb = null;
  4818. state.length -= state.writelen;
  4819. state.writelen = 0;
  4820. }
  4821.  
  4822. function onwrite(stream, er) {
  4823. var state = stream._writableState;
  4824. var sync = state.sync;
  4825. var cb = state.writecb;
  4826.  
  4827. onwriteStateUpdate(state);
  4828.  
  4829. if (er)
  4830. onwriteError(stream, state, sync, er, cb);
  4831. else {
  4832. // Check if we're actually ready to finish, but don't emit yet
  4833. var finished = needFinish(state);
  4834.  
  4835. if (!finished &&
  4836. !state.corked &&
  4837. !state.bufferProcessing &&
  4838. state.bufferedRequest) {
  4839. clearBuffer(stream, state);
  4840. }
  4841.  
  4842. if (sync) {
  4843. processNextTick(afterWrite, stream, state, finished, cb);
  4844. } else {
  4845. afterWrite(stream, state, finished, cb);
  4846. }
  4847. }
  4848. }
  4849.  
  4850. function afterWrite(stream, state, finished, cb) {
  4851. if (!finished)
  4852. onwriteDrain(stream, state);
  4853. state.pendingcb--;
  4854. cb();
  4855. finishMaybe(stream, state);
  4856. }
  4857.  
  4858. // Must force callback to be called on nextTick, so that we don't
  4859. // emit 'drain' before the write() consumer gets the 'false' return
  4860. // value, and has a chance to attach a 'drain' listener.
  4861. function onwriteDrain(stream, state) {
  4862. if (state.length === 0 && state.needDrain) {
  4863. state.needDrain = false;
  4864. stream.emit('drain');
  4865. }
  4866. }
  4867.  
  4868.  
  4869. // if there's something in the buffer waiting, then process it
  4870. function clearBuffer(stream, state) {
  4871. state.bufferProcessing = true;
  4872. var entry = state.bufferedRequest;
  4873.  
  4874. if (stream._writev && entry && entry.next) {
  4875. // Fast case, write everything using _writev()
  4876. var buffer = [];
  4877. var cbs = [];
  4878. while (entry) {
  4879. cbs.push(entry.callback);
  4880. buffer.push(entry);
  4881. entry = entry.next;
  4882. }
  4883.  
  4884. // count the one we are adding, as well.
  4885. // TODO(isaacs) clean this up
  4886. state.pendingcb++;
  4887. state.lastBufferedRequest = null;
  4888. doWrite(stream, state, true, state.length, buffer, '', function(err) {
  4889. for (var i = 0; i < cbs.length; i++) {
  4890. state.pendingcb--;
  4891. cbs[i](err);
  4892. }
  4893. });
  4894.  
  4895. // Clear buffer
  4896. } else {
  4897. // Slow case, write chunks one-by-one
  4898. while (entry) {
  4899. var chunk = entry.chunk;
  4900. var encoding = entry.encoding;
  4901. var cb = entry.callback;
  4902. var len = state.objectMode ? 1 : chunk.length;
  4903.  
  4904. doWrite(stream, state, false, len, chunk, encoding, cb);
  4905. entry = entry.next;
  4906. // if we didn't call the onwrite immediately, then
  4907. // it means that we need to wait until it does.
  4908. // also, that means that the chunk and cb are currently
  4909. // being processed, so move the buffer counter past them.
  4910. if (state.writing) {
  4911. break;
  4912. }
  4913. }
  4914.  
  4915. if (entry === null)
  4916. state.lastBufferedRequest = null;
  4917. }
  4918. state.bufferedRequest = entry;
  4919. state.bufferProcessing = false;
  4920. }
  4921.  
  4922. Writable.prototype._write = function(chunk, encoding, cb) {
  4923. cb(new Error('not implemented'));
  4924. };
  4925.  
  4926. Writable.prototype._writev = null;
  4927.  
  4928. Writable.prototype.end = function(chunk, encoding, cb) {
  4929. var state = this._writableState;
  4930.  
  4931. if (typeof chunk === 'function') {
  4932. cb = chunk;
  4933. chunk = null;
  4934. encoding = null;
  4935. } else if (typeof encoding === 'function') {
  4936. cb = encoding;
  4937. encoding = null;
  4938. }
  4939.  
  4940. if (chunk !== null && chunk !== undefined)
  4941. this.write(chunk, encoding);
  4942.  
  4943. // .end() fully uncorks
  4944. if (state.corked) {
  4945. state.corked = 1;
  4946. this.uncork();
  4947. }
  4948.  
  4949. // ignore unnecessary end() calls.
  4950. if (!state.ending && !state.finished)
  4951. endWritable(this, state, cb);
  4952. };
  4953.  
  4954.  
  4955. function needFinish(state) {
  4956. return (state.ending &&
  4957. state.length === 0 &&
  4958. state.bufferedRequest === null &&
  4959. !state.finished &&
  4960. !state.writing);
  4961. }
  4962.  
  4963. function prefinish(stream, state) {
  4964. if (!state.prefinished) {
  4965. state.prefinished = true;
  4966. stream.emit('prefinish');
  4967. }
  4968. }
  4969.  
  4970. function finishMaybe(stream, state) {
  4971. var need = needFinish(state);
  4972. if (need) {
  4973. if (state.pendingcb === 0) {
  4974. prefinish(stream, state);
  4975. state.finished = true;
  4976. stream.emit('finish');
  4977. } else {
  4978. prefinish(stream, state);
  4979. }
  4980. }
  4981. return need;
  4982. }
  4983.  
  4984. function endWritable(stream, state, cb) {
  4985. state.ending = true;
  4986. finishMaybe(stream, state);
  4987. if (cb) {
  4988. if (state.finished)
  4989. processNextTick(cb);
  4990. else
  4991. stream.once('finish', cb);
  4992. }
  4993. state.ended = true;
  4994. }
  4995.  
  4996. },{"./_stream_duplex":21,"buffer":"buffer","core-util-is":26,"events":15,"inherits":16,"process-nextick-args":27,"util-deprecate":28}],26:[function(require,module,exports){
  4997. (function (Buffer){
  4998. // Copyright Joyent, Inc. and other Node contributors.
  4999. //
  5000. // Permission is hereby granted, free of charge, to any person obtaining a
  5001. // copy of this software and associated documentation files (the
  5002. // "Software"), to deal in the Software without restriction, including
  5003. // without limitation the rights to use, copy, modify, merge, publish,
  5004. // distribute, sublicense, and/or sell copies of the Software, and to permit
  5005. // persons to whom the Software is furnished to do so, subject to the
  5006. // following conditions:
  5007. //
  5008. // The above copyright notice and this permission notice shall be included
  5009. // in all copies or substantial portions of the Software.
  5010. //
  5011. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  5012. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  5013. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  5014. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  5015. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  5016. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  5017. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  5018.  
  5019. // NOTE: These type checking functions intentionally don't use `instanceof`
  5020. // because it is fragile and can be easily faked with `Object.create()`.
  5021. function isArray(ar) {
  5022. return Array.isArray(ar);
  5023. }
  5024. exports.isArray = isArray;
  5025.  
  5026. function isBoolean(arg) {
  5027. return typeof arg === 'boolean';
  5028. }
  5029. exports.isBoolean = isBoolean;
  5030.  
  5031. function isNull(arg) {
  5032. return arg === null;
  5033. }
  5034. exports.isNull = isNull;
  5035.  
  5036. function isNullOrUndefined(arg) {
  5037. return arg == null;
  5038. }
  5039. exports.isNullOrUndefined = isNullOrUndefined;
  5040.  
  5041. function isNumber(arg) {
  5042. return typeof arg === 'number';
  5043. }
  5044. exports.isNumber = isNumber;
  5045.  
  5046. function isString(arg) {
  5047. return typeof arg === 'string';
  5048. }
  5049. exports.isString = isString;
  5050.  
  5051. function isSymbol(arg) {
  5052. return typeof arg === 'symbol';
  5053. }
  5054. exports.isSymbol = isSymbol;
  5055.  
  5056. function isUndefined(arg) {
  5057. return arg === void 0;
  5058. }
  5059. exports.isUndefined = isUndefined;
  5060.  
  5061. function isRegExp(re) {
  5062. return isObject(re) && objectToString(re) === '[object RegExp]';
  5063. }
  5064. exports.isRegExp = isRegExp;
  5065.  
  5066. function isObject(arg) {
  5067. return typeof arg === 'object' && arg !== null;
  5068. }
  5069. exports.isObject = isObject;
  5070.  
  5071. function isDate(d) {
  5072. return isObject(d) && objectToString(d) === '[object Date]';
  5073. }
  5074. exports.isDate = isDate;
  5075.  
  5076. function isError(e) {
  5077. return isObject(e) &&
  5078. (objectToString(e) === '[object Error]' || e instanceof Error);
  5079. }
  5080. exports.isError = isError;
  5081.  
  5082. function isFunction(arg) {
  5083. return typeof arg === 'function';
  5084. }
  5085. exports.isFunction = isFunction;
  5086.  
  5087. function isPrimitive(arg) {
  5088. return arg === null ||
  5089. typeof arg === 'boolean' ||
  5090. typeof arg === 'number' ||
  5091. typeof arg === 'string' ||
  5092. typeof arg === 'symbol' || // ES6 symbol
  5093. typeof arg === 'undefined';
  5094. }
  5095. exports.isPrimitive = isPrimitive;
  5096.  
  5097. function isBuffer(arg) {
  5098. return Buffer.isBuffer(arg);
  5099. }
  5100. exports.isBuffer = isBuffer;
  5101.  
  5102. function objectToString(o) {
  5103. return Object.prototype.toString.call(o);
  5104. }
  5105. }).call(this,{"isBuffer":require("../../../../insert-module-globals/node_modules/is-buffer/index.js")})
  5106. },{"../../../../insert-module-globals/node_modules/is-buffer/index.js":17}],27:[function(require,module,exports){
  5107. (function (process){
  5108. 'use strict';
  5109. module.exports = nextTick;
  5110.  
  5111. function nextTick(fn) {
  5112. var args = new Array(arguments.length - 1);
  5113. var i = 0;
  5114. while (i < args.length) {
  5115. args[i++] = arguments[i];
  5116. }
  5117. process.nextTick(function afterTick() {
  5118. fn.apply(null, args);
  5119. });
  5120. }
  5121.  
  5122. }).call(this,require('_process'))
  5123. },{"_process":19}],28:[function(require,module,exports){
  5124. (function (global){
  5125.  
  5126. /**
  5127. * Module exports.
  5128. */
  5129.  
  5130. module.exports = deprecate;
  5131.  
  5132. /**
  5133. * Mark that a method should not be used.
  5134. * Returns a modified function which warns once by default.
  5135. *
  5136. * If `localStorage.noDeprecation = true` is set, then it is a no-op.
  5137. *
  5138. * If `localStorage.throwDeprecation = true` is set, then deprecated functions
  5139. * will throw an Error when invoked.
  5140. *
  5141. * If `localStorage.traceDeprecation = true` is set, then deprecated functions
  5142. * will invoke `console.trace()` instead of `console.error()`.
  5143. *
  5144. * @param {Function} fn - the function to deprecate
  5145. * @param {String} msg - the string to print to the console when `fn` is invoked
  5146. * @returns {Function} a new "deprecated" version of `fn`
  5147. * @api public
  5148. */
  5149.  
  5150. function deprecate (fn, msg) {
  5151. if (config('noDeprecation')) {
  5152. return fn;
  5153. }
  5154.  
  5155. var warned = false;
  5156. function deprecated() {
  5157. if (!warned) {
  5158. if (config('throwDeprecation')) {
  5159. throw new Error(msg);
  5160. } else if (config('traceDeprecation')) {
  5161. console.trace(msg);
  5162. } else {
  5163. console.warn(msg);
  5164. }
  5165. warned = true;
  5166. }
  5167. return fn.apply(this, arguments);
  5168. }
  5169.  
  5170. return deprecated;
  5171. }
  5172.  
  5173. /**
  5174. * Checks `localStorage` for boolean values for the given `name`.
  5175. *
  5176. * @param {String} name
  5177. * @returns {Boolean}
  5178. * @api private
  5179. */
  5180.  
  5181. function config (name) {
  5182. // accessing global.localStorage can trigger a DOMException in sandboxed iframes
  5183. try {
  5184. if (!global.localStorage) return false;
  5185. } catch (_) {
  5186. return false;
  5187. }
  5188. var val = global.localStorage[name];
  5189. if (null == val) return false;
  5190. return String(val).toLowerCase() === 'true';
  5191. }
  5192.  
  5193. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  5194. },{}],29:[function(require,module,exports){
  5195. module.exports = require("./lib/_stream_passthrough.js")
  5196.  
  5197. },{"./lib/_stream_passthrough.js":22}],30:[function(require,module,exports){
  5198. var Stream = (function (){
  5199. try {
  5200. return require('st' + 'ream'); // hack to fix a circular dependency issue when used with browserify
  5201. } catch(_){}
  5202. }());
  5203. exports = module.exports = require('./lib/_stream_readable.js');
  5204. exports.Stream = Stream || exports;
  5205. exports.Readable = exports;
  5206. exports.Writable = require('./lib/_stream_writable.js');
  5207. exports.Duplex = require('./lib/_stream_duplex.js');
  5208. exports.Transform = require('./lib/_stream_transform.js');
  5209. exports.PassThrough = require('./lib/_stream_passthrough.js');
  5210.  
  5211. },{"./lib/_stream_duplex.js":21,"./lib/_stream_passthrough.js":22,"./lib/_stream_readable.js":23,"./lib/_stream_transform.js":24,"./lib/_stream_writable.js":25}],31:[function(require,module,exports){
  5212. module.exports = require("./lib/_stream_transform.js")
  5213.  
  5214. },{"./lib/_stream_transform.js":24}],32:[function(require,module,exports){
  5215. module.exports = require("./lib/_stream_writable.js")
  5216.  
  5217. },{"./lib/_stream_writable.js":25}],33:[function(require,module,exports){
  5218. // Copyright Joyent, Inc. and other Node contributors.
  5219. //
  5220. // Permission is hereby granted, free of charge, to any person obtaining a
  5221. // copy of this software and associated documentation files (the
  5222. // "Software"), to deal in the Software without restriction, including
  5223. // without limitation the rights to use, copy, modify, merge, publish,
  5224. // distribute, sublicense, and/or sell copies of the Software, and to permit
  5225. // persons to whom the Software is furnished to do so, subject to the
  5226. // following conditions:
  5227. //
  5228. // The above copyright notice and this permission notice shall be included
  5229. // in all copies or substantial portions of the Software.
  5230. //
  5231. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  5232. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  5233. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  5234. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  5235. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  5236. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  5237. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  5238.  
  5239. module.exports = Stream;
  5240.  
  5241. var EE = require('events').EventEmitter;
  5242. var inherits = require('inherits');
  5243.  
  5244. inherits(Stream, EE);
  5245. Stream.Readable = require('readable-stream/readable.js');
  5246. Stream.Writable = require('readable-stream/writable.js');
  5247. Stream.Duplex = require('readable-stream/duplex.js');
  5248. Stream.Transform = require('readable-stream/transform.js');
  5249. Stream.PassThrough = require('readable-stream/passthrough.js');
  5250.  
  5251. // Backwards-compat with node 0.4.x
  5252. Stream.Stream = Stream;
  5253.  
  5254.  
  5255.  
  5256. // old-style streams. Note that the pipe method (the only relevant
  5257. // part of this class) is overridden in the Readable class.
  5258.  
  5259. function Stream() {
  5260. EE.call(this);
  5261. }
  5262.  
  5263. Stream.prototype.pipe = function(dest, options) {
  5264. var source = this;
  5265.  
  5266. function ondata(chunk) {
  5267. if (dest.writable) {
  5268. if (false === dest.write(chunk) && source.pause) {
  5269. source.pause();
  5270. }
  5271. }
  5272. }
  5273.  
  5274. source.on('data', ondata);
  5275.  
  5276. function ondrain() {
  5277. if (source.readable && source.resume) {
  5278. source.resume();
  5279. }
  5280. }
  5281.  
  5282. dest.on('drain', ondrain);
  5283.  
  5284. // If the 'end' option is not supplied, dest.end() will be called when
  5285. // source gets the 'end' or 'close' events. Only dest.end() once.
  5286. if (!dest._isStdio && (!options || options.end !== false)) {
  5287. source.on('end', onend);
  5288. source.on('close', onclose);
  5289. }
  5290.  
  5291. var didOnEnd = false;
  5292. function onend() {
  5293. if (didOnEnd) return;
  5294. didOnEnd = true;
  5295.  
  5296. dest.end();
  5297. }
  5298.  
  5299.  
  5300. function onclose() {
  5301. if (didOnEnd) return;
  5302. didOnEnd = true;
  5303.  
  5304. if (typeof dest.destroy === 'function') dest.destroy();
  5305. }
  5306.  
  5307. // don't leave dangling pipes when there are errors.
  5308. function onerror(er) {
  5309. cleanup();
  5310. if (EE.listenerCount(this, 'error') === 0) {
  5311. throw er; // Unhandled stream error in pipe.
  5312. }
  5313. }
  5314.  
  5315. source.on('error', onerror);
  5316. dest.on('error', onerror);
  5317.  
  5318. // remove all the event listeners that were added.
  5319. function cleanup() {
  5320. source.removeListener('data', ondata);
  5321. dest.removeListener('drain', ondrain);
  5322.  
  5323. source.removeListener('end', onend);
  5324. source.removeListener('close', onclose);
  5325.  
  5326. source.removeListener('error', onerror);
  5327. dest.removeListener('error', onerror);
  5328.  
  5329. source.removeListener('end', cleanup);
  5330. source.removeListener('close', cleanup);
  5331.  
  5332. dest.removeListener('close', cleanup);
  5333. }
  5334.  
  5335. source.on('end', cleanup);
  5336. source.on('close', cleanup);
  5337.  
  5338. dest.on('close', cleanup);
  5339.  
  5340. dest.emit('pipe', source);
  5341.  
  5342. // Allow for unix-like usage: A.pipe(B).pipe(C)
  5343. return dest;
  5344. };
  5345.  
  5346. },{"events":15,"inherits":16,"readable-stream/duplex.js":20,"readable-stream/passthrough.js":29,"readable-stream/readable.js":30,"readable-stream/transform.js":31,"readable-stream/writable.js":32}],34:[function(require,module,exports){
  5347. // Copyright Joyent, Inc. and other Node contributors.
  5348. //
  5349. // Permission is hereby granted, free of charge, to any person obtaining a
  5350. // copy of this software and associated documentation files (the
  5351. // "Software"), to deal in the Software without restriction, including
  5352. // without limitation the rights to use, copy, modify, merge, publish,
  5353. // distribute, sublicense, and/or sell copies of the Software, and to permit
  5354. // persons to whom the Software is furnished to do so, subject to the
  5355. // following conditions:
  5356. //
  5357. // The above copyright notice and this permission notice shall be included
  5358. // in all copies or substantial portions of the Software.
  5359. //
  5360. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  5361. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  5362. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  5363. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  5364. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  5365. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  5366. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  5367.  
  5368. var Buffer = require('buffer').Buffer;
  5369.  
  5370. var isBufferEncoding = Buffer.isEncoding
  5371. || function(encoding) {
  5372. switch (encoding && encoding.toLowerCase()) {
  5373. 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;
  5374. default: return false;
  5375. }
  5376. }
  5377.  
  5378.  
  5379. function assertEncoding(encoding) {
  5380. if (encoding && !isBufferEncoding(encoding)) {
  5381. throw new Error('Unknown encoding: ' + encoding);
  5382. }
  5383. }
  5384.  
  5385. // StringDecoder provides an interface for efficiently splitting a series of
  5386. // buffers into a series of JS strings without breaking apart multi-byte
  5387. // characters. CESU-8 is handled as part of the UTF-8 encoding.
  5388. //
  5389. // @TODO Handling all encodings inside a single object makes it very difficult
  5390. // to reason about this code, so it should be split up in the future.
  5391. // @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code
  5392. // points as used by CESU-8.
  5393. var StringDecoder = exports.StringDecoder = function(encoding) {
  5394. this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, '');
  5395. assertEncoding(encoding);
  5396. switch (this.encoding) {
  5397. case 'utf8':
  5398. // CESU-8 represents each of Surrogate Pair by 3-bytes
  5399. this.surrogateSize = 3;
  5400. break;
  5401. case 'ucs2':
  5402. case 'utf16le':
  5403. // UTF-16 represents each of Surrogate Pair by 2-bytes
  5404. this.surrogateSize = 2;
  5405. this.detectIncompleteChar = utf16DetectIncompleteChar;
  5406. break;
  5407. case 'base64':
  5408. // Base-64 stores 3 bytes in 4 chars, and pads the remainder.
  5409. this.surrogateSize = 3;
  5410. this.detectIncompleteChar = base64DetectIncompleteChar;
  5411. break;
  5412. default:
  5413. this.write = passThroughWrite;
  5414. return;
  5415. }
  5416.  
  5417. // Enough space to store all bytes of a single character. UTF-8 needs 4
  5418. // bytes, but CESU-8 may require up to 6 (3 bytes per surrogate).
  5419. this.charBuffer = new Buffer(6);
  5420. // Number of bytes received for the current incomplete multi-byte character.
  5421. this.charReceived = 0;
  5422. // Number of bytes expected for the current incomplete multi-byte character.
  5423. this.charLength = 0;
  5424. };
  5425.  
  5426.  
  5427. // write decodes the given buffer and returns it as JS string that is
  5428. // guaranteed to not contain any partial multi-byte characters. Any partial
  5429. // character found at the end of the buffer is buffered up, and will be
  5430. // returned when calling write again with the remaining bytes.
  5431. //
  5432. // Note: Converting a Buffer containing an orphan surrogate to a String
  5433. // currently works, but converting a String to a Buffer (via `new Buffer`, or
  5434. // Buffer#write) will replace incomplete surrogates with the unicode
  5435. // replacement character. See https://codereview.chromium.org/121173009/ .
  5436. StringDecoder.prototype.write = function(buffer) {
  5437. var charStr = '';
  5438. // if our last write ended with an incomplete multibyte character
  5439. while (this.charLength) {
  5440. // determine how many remaining bytes this buffer has to offer for this char
  5441. var available = (buffer.length >= this.charLength - this.charReceived) ?
  5442. this.charLength - this.charReceived :
  5443. buffer.length;
  5444.  
  5445. // add the new bytes to the char buffer
  5446. buffer.copy(this.charBuffer, this.charReceived, 0, available);
  5447. this.charReceived += available;
  5448.  
  5449. if (this.charReceived < this.charLength) {
  5450. // still not enough chars in this buffer? wait for more ...
  5451. return '';
  5452. }
  5453.  
  5454. // remove bytes belonging to the current character from the buffer
  5455. buffer = buffer.slice(available, buffer.length);
  5456.  
  5457. // get the character that was split
  5458. charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding);
  5459.  
  5460. // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
  5461. var charCode = charStr.charCodeAt(charStr.length - 1);
  5462. if (charCode >= 0xD800 && charCode <= 0xDBFF) {
  5463. this.charLength += this.surrogateSize;
  5464. charStr = '';
  5465. continue;
  5466. }
  5467. this.charReceived = this.charLength = 0;
  5468.  
  5469. // if there are no more bytes in this buffer, just emit our char
  5470. if (buffer.length === 0) {
  5471. return charStr;
  5472. }
  5473. break;
  5474. }
  5475.  
  5476. // determine and set charLength / charReceived
  5477. this.detectIncompleteChar(buffer);
  5478.  
  5479. var end = buffer.length;
  5480. if (this.charLength) {
  5481. // buffer the incomplete character bytes we got
  5482. buffer.copy(this.charBuffer, 0, buffer.length - this.charReceived, end);
  5483. end -= this.charReceived;
  5484. }
  5485.  
  5486. charStr += buffer.toString(this.encoding, 0, end);
  5487.  
  5488. var end = charStr.length - 1;
  5489. var charCode = charStr.charCodeAt(end);
  5490. // CESU-8: lead surrogate (D800-DBFF) is also the incomplete character
  5491. if (charCode >= 0xD800 && charCode <= 0xDBFF) {
  5492. var size = this.surrogateSize;
  5493. this.charLength += size;
  5494. this.charReceived += size;
  5495. this.charBuffer.copy(this.charBuffer, size, 0, size);
  5496. buffer.copy(this.charBuffer, 0, 0, size);
  5497. return charStr.substring(0, end);
  5498. }
  5499.  
  5500. // or just emit the charStr
  5501. return charStr;
  5502. };
  5503.  
  5504. // detectIncompleteChar determines if there is an incomplete UTF-8 character at
  5505. // the end of the given buffer. If so, it sets this.charLength to the byte
  5506. // length that character, and sets this.charReceived to the number of bytes
  5507. // that are available for this character.
  5508. StringDecoder.prototype.detectIncompleteChar = function(buffer) {
  5509. // determine how many bytes we have to check at the end of this buffer
  5510. var i = (buffer.length >= 3) ? 3 : buffer.length;
  5511.  
  5512. // Figure out if one of the last i bytes of our buffer announces an
  5513. // incomplete char.
  5514. for (; i > 0; i--) {
  5515. var c = buffer[buffer.length - i];
  5516.  
  5517. // See http://en.wikipedia.org/wiki/UTF-8#Description
  5518.  
  5519. // 110XXXXX
  5520. if (i == 1 && c >> 5 == 0x06) {
  5521. this.charLength = 2;
  5522. break;
  5523. }
  5524.  
  5525. // 1110XXXX
  5526. if (i <= 2 && c >> 4 == 0x0E) {
  5527. this.charLength = 3;
  5528. break;
  5529. }
  5530.  
  5531. // 11110XXX
  5532. if (i <= 3 && c >> 3 == 0x1E) {
  5533. this.charLength = 4;
  5534. break;
  5535. }
  5536. }
  5537. this.charReceived = i;
  5538. };
  5539.  
  5540. StringDecoder.prototype.end = function(buffer) {
  5541. var res = '';
  5542. if (buffer && buffer.length)
  5543. res = this.write(buffer);
  5544.  
  5545. if (this.charReceived) {
  5546. var cr = this.charReceived;
  5547. var buf = this.charBuffer;
  5548. var enc = this.encoding;
  5549. res += buf.slice(0, cr).toString(enc);
  5550. }
  5551.  
  5552. return res;
  5553. };
  5554.  
  5555. function passThroughWrite(buffer) {
  5556. return buffer.toString(this.encoding);
  5557. }
  5558.  
  5559. function utf16DetectIncompleteChar(buffer) {
  5560. this.charReceived = buffer.length % 2;
  5561. this.charLength = this.charReceived ? 2 : 0;
  5562. }
  5563.  
  5564. function base64DetectIncompleteChar(buffer) {
  5565. this.charReceived = buffer.length % 3;
  5566. this.charLength = this.charReceived ? 3 : 0;
  5567. }
  5568.  
  5569. },{"buffer":"buffer"}],35:[function(require,module,exports){
  5570. module.exports = function isBuffer(arg) {
  5571. return arg && typeof arg === 'object'
  5572. && typeof arg.copy === 'function'
  5573. && typeof arg.fill === 'function'
  5574. && typeof arg.readUInt8 === 'function';
  5575. }
  5576. },{}],36:[function(require,module,exports){
  5577. (function (process,global){
  5578. // Copyright Joyent, Inc. and other Node contributors.
  5579. //
  5580. // Permission is hereby granted, free of charge, to any person obtaining a
  5581. // copy of this software and associated documentation files (the
  5582. // "Software"), to deal in the Software without restriction, including
  5583. // without limitation the rights to use, copy, modify, merge, publish,
  5584. // distribute, sublicense, and/or sell copies of the Software, and to permit
  5585. // persons to whom the Software is furnished to do so, subject to the
  5586. // following conditions:
  5587. //
  5588. // The above copyright notice and this permission notice shall be included
  5589. // in all copies or substantial portions of the Software.
  5590. //
  5591. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  5592. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  5593. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  5594. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  5595. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  5596. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  5597. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  5598.  
  5599. var formatRegExp = /%[sdj%]/g;
  5600. exports.format = function(f) {
  5601. if (!isString(f)) {
  5602. var objects = [];
  5603. for (var i = 0; i < arguments.length; i++) {
  5604. objects.push(inspect(arguments[i]));
  5605. }
  5606. return objects.join(' ');
  5607. }
  5608.  
  5609. var i = 1;
  5610. var args = arguments;
  5611. var len = args.length;
  5612. var str = String(f).replace(formatRegExp, function(x) {
  5613. if (x === '%%') return '%';
  5614. if (i >= len) return x;
  5615. switch (x) {
  5616. case '%s': return String(args[i++]);
  5617. case '%d': return Number(args[i++]);
  5618. case '%j':
  5619. try {
  5620. return JSON.stringify(args[i++]);
  5621. } catch (_) {
  5622. return '[Circular]';
  5623. }
  5624. default:
  5625. return x;
  5626. }
  5627. });
  5628. for (var x = args[i]; i < len; x = args[++i]) {
  5629. if (isNull(x) || !isObject(x)) {
  5630. str += ' ' + x;
  5631. } else {
  5632. str += ' ' + inspect(x);
  5633. }
  5634. }
  5635. return str;
  5636. };
  5637.  
  5638.  
  5639. // Mark that a method should not be used.
  5640. // Returns a modified function which warns once by default.
  5641. // If --no-deprecation is set, then it is a no-op.
  5642. exports.deprecate = function(fn, msg) {
  5643. // Allow for deprecating things in the process of starting up.
  5644. if (isUndefined(global.process)) {
  5645. return function() {
  5646. return exports.deprecate(fn, msg).apply(this, arguments);
  5647. };
  5648. }
  5649.  
  5650. if (process.noDeprecation === true) {
  5651. return fn;
  5652. }
  5653.  
  5654. var warned = false;
  5655. function deprecated() {
  5656. if (!warned) {
  5657. if (process.throwDeprecation) {
  5658. throw new Error(msg);
  5659. } else if (process.traceDeprecation) {
  5660. console.trace(msg);
  5661. } else {
  5662. console.error(msg);
  5663. }
  5664. warned = true;
  5665. }
  5666. return fn.apply(this, arguments);
  5667. }
  5668.  
  5669. return deprecated;
  5670. };
  5671.  
  5672.  
  5673. var debugs = {};
  5674. var debugEnviron;
  5675. exports.debuglog = function(set) {
  5676. if (isUndefined(debugEnviron))
  5677. debugEnviron = process.env.NODE_DEBUG || '';
  5678. set = set.toUpperCase();
  5679. if (!debugs[set]) {
  5680. if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
  5681. var pid = process.pid;
  5682. debugs[set] = function() {
  5683. var msg = exports.format.apply(exports, arguments);
  5684. console.error('%s %d: %s', set, pid, msg);
  5685. };
  5686. } else {
  5687. debugs[set] = function() {};
  5688. }
  5689. }
  5690. return debugs[set];
  5691. };
  5692.  
  5693.  
  5694. /**
  5695. * Echos the value of a value. Trys to print the value out
  5696. * in the best way possible given the different types.
  5697. *
  5698. * @param {Object} obj The object to print out.
  5699. * @param {Object} opts Optional options object that alters the output.
  5700. */
  5701. /* legacy: obj, showHidden, depth, colors*/
  5702. function inspect(obj, opts) {
  5703. // default options
  5704. var ctx = {
  5705. seen: [],
  5706. stylize: stylizeNoColor
  5707. };
  5708. // legacy...
  5709. if (arguments.length >= 3) ctx.depth = arguments[2];
  5710. if (arguments.length >= 4) ctx.colors = arguments[3];
  5711. if (isBoolean(opts)) {
  5712. // legacy...
  5713. ctx.showHidden = opts;
  5714. } else if (opts) {
  5715. // got an "options" object
  5716. exports._extend(ctx, opts);
  5717. }
  5718. // set default options
  5719. if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
  5720. if (isUndefined(ctx.depth)) ctx.depth = 2;
  5721. if (isUndefined(ctx.colors)) ctx.colors = false;
  5722. if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
  5723. if (ctx.colors) ctx.stylize = stylizeWithColor;
  5724. return formatValue(ctx, obj, ctx.depth);
  5725. }
  5726. exports.inspect = inspect;
  5727.  
  5728.  
  5729. // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
  5730. inspect.colors = {
  5731. 'bold' : [1, 22],
  5732. 'italic' : [3, 23],
  5733. 'underline' : [4, 24],
  5734. 'inverse' : [7, 27],
  5735. 'white' : [37, 39],
  5736. 'grey' : [90, 39],
  5737. 'black' : [30, 39],
  5738. 'blue' : [34, 39],
  5739. 'cyan' : [36, 39],
  5740. 'green' : [32, 39],
  5741. 'magenta' : [35, 39],
  5742. 'red' : [31, 39],
  5743. 'yellow' : [33, 39]
  5744. };
  5745.  
  5746. // Don't use 'blue' not visible on cmd.exe
  5747. inspect.styles = {
  5748. 'special': 'cyan',
  5749. 'number': 'yellow',
  5750. 'boolean': 'yellow',
  5751. 'undefined': 'grey',
  5752. 'null': 'bold',
  5753. 'string': 'green',
  5754. 'date': 'magenta',
  5755. // "name": intentionally not styling
  5756. 'regexp': 'red'
  5757. };
  5758.  
  5759.  
  5760. function stylizeWithColor(str, styleType) {
  5761. var style = inspect.styles[styleType];
  5762.  
  5763. if (style) {
  5764. return '\u001b[' + inspect.colors[style][0] + 'm' + str +
  5765. '\u001b[' + inspect.colors[style][1] + 'm';
  5766. } else {
  5767. return str;
  5768. }
  5769. }
  5770.  
  5771.  
  5772. function stylizeNoColor(str, styleType) {
  5773. return str;
  5774. }
  5775.  
  5776.  
  5777. function arrayToHash(array) {
  5778. var hash = {};
  5779.  
  5780. array.forEach(function(val, idx) {
  5781. hash[val] = true;
  5782. });
  5783.  
  5784. return hash;
  5785. }
  5786.  
  5787.  
  5788. function formatValue(ctx, value, recurseTimes) {
  5789. // Provide a hook for user-specified inspect functions.
  5790. // Check that value is an object with an inspect function on it
  5791. if (ctx.customInspect &&
  5792. value &&
  5793. isFunction(value.inspect) &&
  5794. // Filter out the util module, it's inspect function is special
  5795. value.inspect !== exports.inspect &&
  5796. // Also filter out any prototype objects using the circular check.
  5797. !(value.constructor && value.constructor.prototype === value)) {
  5798. var ret = value.inspect(recurseTimes, ctx);
  5799. if (!isString(ret)) {
  5800. ret = formatValue(ctx, ret, recurseTimes);
  5801. }
  5802. return ret;
  5803. }
  5804.  
  5805. // Primitive types cannot have properties
  5806. var primitive = formatPrimitive(ctx, value);
  5807. if (primitive) {
  5808. return primitive;
  5809. }
  5810.  
  5811. // Look up the keys of the object.
  5812. var keys = Object.keys(value);
  5813. var visibleKeys = arrayToHash(keys);
  5814.  
  5815. if (ctx.showHidden) {
  5816. keys = Object.getOwnPropertyNames(value);
  5817. }
  5818.  
  5819. // IE doesn't make error fields non-enumerable
  5820. // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
  5821. if (isError(value)
  5822. && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
  5823. return formatError(value);
  5824. }
  5825.  
  5826. // Some type of object without properties can be shortcutted.
  5827. if (keys.length === 0) {
  5828. if (isFunction(value)) {
  5829. var name = value.name ? ': ' + value.name : '';
  5830. return ctx.stylize('[Function' + name + ']', 'special');
  5831. }
  5832. if (isRegExp(value)) {
  5833. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  5834. }
  5835. if (isDate(value)) {
  5836. return ctx.stylize(Date.prototype.toString.call(value), 'date');
  5837. }
  5838. if (isError(value)) {
  5839. return formatError(value);
  5840. }
  5841. }
  5842.  
  5843. var base = '', array = false, braces = ['{', '}'];
  5844.  
  5845. // Make Array say that they are Array
  5846. if (isArray(value)) {
  5847. array = true;
  5848. braces = ['[', ']'];
  5849. }
  5850.  
  5851. // Make functions say that they are functions
  5852. if (isFunction(value)) {
  5853. var n = value.name ? ': ' + value.name : '';
  5854. base = ' [Function' + n + ']';
  5855. }
  5856.  
  5857. // Make RegExps say that they are RegExps
  5858. if (isRegExp(value)) {
  5859. base = ' ' + RegExp.prototype.toString.call(value);
  5860. }
  5861.  
  5862. // Make dates with properties first say the date
  5863. if (isDate(value)) {
  5864. base = ' ' + Date.prototype.toUTCString.call(value);
  5865. }
  5866.  
  5867. // Make error with message first say the error
  5868. if (isError(value)) {
  5869. base = ' ' + formatError(value);
  5870. }
  5871.  
  5872. if (keys.length === 0 && (!array || value.length == 0)) {
  5873. return braces[0] + base + braces[1];
  5874. }
  5875.  
  5876. if (recurseTimes < 0) {
  5877. if (isRegExp(value)) {
  5878. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  5879. } else {
  5880. return ctx.stylize('[Object]', 'special');
  5881. }
  5882. }
  5883.  
  5884. ctx.seen.push(value);
  5885.  
  5886. var output;
  5887. if (array) {
  5888. output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
  5889. } else {
  5890. output = keys.map(function(key) {
  5891. return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
  5892. });
  5893. }
  5894.  
  5895. ctx.seen.pop();
  5896.  
  5897. return reduceToSingleString(output, base, braces);
  5898. }
  5899.  
  5900.  
  5901. function formatPrimitive(ctx, value) {
  5902. if (isUndefined(value))
  5903. return ctx.stylize('undefined', 'undefined');
  5904. if (isString(value)) {
  5905. var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
  5906. .replace(/'/g, "\\'")
  5907. .replace(/\\"/g, '"') + '\'';
  5908. return ctx.stylize(simple, 'string');
  5909. }
  5910. if (isNumber(value))
  5911. return ctx.stylize('' + value, 'number');
  5912. if (isBoolean(value))
  5913. return ctx.stylize('' + value, 'boolean');
  5914. // For some reason typeof null is "object", so special case here.
  5915. if (isNull(value))
  5916. return ctx.stylize('null', 'null');
  5917. }
  5918.  
  5919.  
  5920. function formatError(value) {
  5921. return '[' + Error.prototype.toString.call(value) + ']';
  5922. }
  5923.  
  5924.  
  5925. function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
  5926. var output = [];
  5927. for (var i = 0, l = value.length; i < l; ++i) {
  5928. if (hasOwnProperty(value, String(i))) {
  5929. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  5930. String(i), true));
  5931. } else {
  5932. output.push('');
  5933. }
  5934. }
  5935. keys.forEach(function(key) {
  5936. if (!key.match(/^\d+$/)) {
  5937. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  5938. key, true));
  5939. }
  5940. });
  5941. return output;
  5942. }
  5943.  
  5944.  
  5945. function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
  5946. var name, str, desc;
  5947. desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
  5948. if (desc.get) {
  5949. if (desc.set) {
  5950. str = ctx.stylize('[Getter/Setter]', 'special');
  5951. } else {
  5952. str = ctx.stylize('[Getter]', 'special');
  5953. }
  5954. } else {
  5955. if (desc.set) {
  5956. str = ctx.stylize('[Setter]', 'special');
  5957. }
  5958. }
  5959. if (!hasOwnProperty(visibleKeys, key)) {
  5960. name = '[' + key + ']';
  5961. }
  5962. if (!str) {
  5963. if (ctx.seen.indexOf(desc.value) < 0) {
  5964. if (isNull(recurseTimes)) {
  5965. str = formatValue(ctx, desc.value, null);
  5966. } else {
  5967. str = formatValue(ctx, desc.value, recurseTimes - 1);
  5968. }
  5969. if (str.indexOf('\n') > -1) {
  5970. if (array) {
  5971. str = str.split('\n').map(function(line) {
  5972. return ' ' + line;
  5973. }).join('\n').substr(2);
  5974. } else {
  5975. str = '\n' + str.split('\n').map(function(line) {
  5976. return ' ' + line;
  5977. }).join('\n');
  5978. }
  5979. }
  5980. } else {
  5981. str = ctx.stylize('[Circular]', 'special');
  5982. }
  5983. }
  5984. if (isUndefined(name)) {
  5985. if (array && key.match(/^\d+$/)) {
  5986. return str;
  5987. }
  5988. name = JSON.stringify('' + key);
  5989. if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
  5990. name = name.substr(1, name.length - 2);
  5991. name = ctx.stylize(name, 'name');
  5992. } else {
  5993. name = name.replace(/'/g, "\\'")
  5994. .replace(/\\"/g, '"')
  5995. .replace(/(^"|"$)/g, "'");
  5996. name = ctx.stylize(name, 'string');
  5997. }
  5998. }
  5999.  
  6000. return name + ': ' + str;
  6001. }
  6002.  
  6003.  
  6004. function reduceToSingleString(output, base, braces) {
  6005. var numLinesEst = 0;
  6006. var length = output.reduce(function(prev, cur) {
  6007. numLinesEst++;
  6008. if (cur.indexOf('\n') >= 0) numLinesEst++;
  6009. return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
  6010. }, 0);
  6011.  
  6012. if (length > 60) {
  6013. return braces[0] +
  6014. (base === '' ? '' : base + '\n ') +
  6015. ' ' +
  6016. output.join(',\n ') +
  6017. ' ' +
  6018. braces[1];
  6019. }
  6020.  
  6021. return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
  6022. }
  6023.  
  6024.  
  6025. // NOTE: These type checking functions intentionally don't use `instanceof`
  6026. // because it is fragile and can be easily faked with `Object.create()`.
  6027. function isArray(ar) {
  6028. return Array.isArray(ar);
  6029. }
  6030. exports.isArray = isArray;
  6031.  
  6032. function isBoolean(arg) {
  6033. return typeof arg === 'boolean';
  6034. }
  6035. exports.isBoolean = isBoolean;
  6036.  
  6037. function isNull(arg) {
  6038. return arg === null;
  6039. }
  6040. exports.isNull = isNull;
  6041.  
  6042. function isNullOrUndefined(arg) {
  6043. return arg == null;
  6044. }
  6045. exports.isNullOrUndefined = isNullOrUndefined;
  6046.  
  6047. function isNumber(arg) {
  6048. return typeof arg === 'number';
  6049. }
  6050. exports.isNumber = isNumber;
  6051.  
  6052. function isString(arg) {
  6053. return typeof arg === 'string';
  6054. }
  6055. exports.isString = isString;
  6056.  
  6057. function isSymbol(arg) {
  6058. return typeof arg === 'symbol';
  6059. }
  6060. exports.isSymbol = isSymbol;
  6061.  
  6062. function isUndefined(arg) {
  6063. return arg === void 0;
  6064. }
  6065. exports.isUndefined = isUndefined;
  6066.  
  6067. function isRegExp(re) {
  6068. return isObject(re) && objectToString(re) === '[object RegExp]';
  6069. }
  6070. exports.isRegExp = isRegExp;
  6071.  
  6072. function isObject(arg) {
  6073. return typeof arg === 'object' && arg !== null;
  6074. }
  6075. exports.isObject = isObject;
  6076.  
  6077. function isDate(d) {
  6078. return isObject(d) && objectToString(d) === '[object Date]';
  6079. }
  6080. exports.isDate = isDate;
  6081.  
  6082. function isError(e) {
  6083. return isObject(e) &&
  6084. (objectToString(e) === '[object Error]' || e instanceof Error);
  6085. }
  6086. exports.isError = isError;
  6087.  
  6088. function isFunction(arg) {
  6089. return typeof arg === 'function';
  6090. }
  6091. exports.isFunction = isFunction;
  6092.  
  6093. function isPrimitive(arg) {
  6094. return arg === null ||
  6095. typeof arg === 'boolean' ||
  6096. typeof arg === 'number' ||
  6097. typeof arg === 'string' ||
  6098. typeof arg === 'symbol' || // ES6 symbol
  6099. typeof arg === 'undefined';
  6100. }
  6101. exports.isPrimitive = isPrimitive;
  6102.  
  6103. exports.isBuffer = require('./support/isBuffer');
  6104.  
  6105. function objectToString(o) {
  6106. return Object.prototype.toString.call(o);
  6107. }
  6108.  
  6109.  
  6110. function pad(n) {
  6111. return n < 10 ? '0' + n.toString(10) : n.toString(10);
  6112. }
  6113.  
  6114.  
  6115. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
  6116. 'Oct', 'Nov', 'Dec'];
  6117.  
  6118. // 26 Feb 16:19:34
  6119. function timestamp() {
  6120. var d = new Date();
  6121. var time = [pad(d.getHours()),
  6122. pad(d.getMinutes()),
  6123. pad(d.getSeconds())].join(':');
  6124. return [d.getDate(), months[d.getMonth()], time].join(' ');
  6125. }
  6126.  
  6127.  
  6128. // log is just a thin wrapper to console.log that prepends a timestamp
  6129. exports.log = function() {
  6130. console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
  6131. };
  6132.  
  6133.  
  6134. /**
  6135. * Inherit the prototype methods from one constructor into another.
  6136. *
  6137. * The Function.prototype.inherits from lang.js rewritten as a standalone
  6138. * function (not on Function.prototype). NOTE: If this file is to be loaded
  6139. * during bootstrapping this function needs to be rewritten using some native
  6140. * functions as prototype setup using normal JavaScript does not work as
  6141. * expected during bootstrapping (see mirror.js in r114903).
  6142. *
  6143. * @param {function} ctor Constructor function which needs to inherit the
  6144. * prototype.
  6145. * @param {function} superCtor Constructor function to inherit prototype from.
  6146. */
  6147. exports.inherits = require('inherits');
  6148.  
  6149. exports._extend = function(origin, add) {
  6150. // Don't do anything if add isn't an object
  6151. if (!add || !isObject(add)) return origin;
  6152.  
  6153. var keys = Object.keys(add);
  6154. var i = keys.length;
  6155. while (i--) {
  6156. origin[keys[i]] = add[keys[i]];
  6157. }
  6158. return origin;
  6159. };
  6160.  
  6161. function hasOwnProperty(obj, prop) {
  6162. return Object.prototype.hasOwnProperty.call(obj, prop);
  6163. }
  6164.  
  6165. }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  6166. },{"./support/isBuffer":35,"_process":19,"inherits":16}],"buffer":[function(require,module,exports){
  6167. (function (global){
  6168. /*!
  6169. * The buffer module from node.js, for the browser.
  6170. *
  6171. * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
  6172. * @license MIT
  6173. */
  6174. /* eslint-disable no-proto */
  6175.  
  6176. var base64 = require('base64-js')
  6177. var ieee754 = require('ieee754')
  6178. var isArray = require('isarray')
  6179.  
  6180. exports.Buffer = Buffer
  6181. exports.SlowBuffer = SlowBuffer
  6182. exports.INSPECT_MAX_BYTES = 50
  6183.  
  6184. /**
  6185. * If `Buffer.TYPED_ARRAY_SUPPORT`:
  6186. * === true Use Uint8Array implementation (fastest)
  6187. * === false Use Object implementation (most compatible, even IE6)
  6188. *
  6189. * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
  6190. * Opera 11.6+, iOS 4.2+.
  6191. *
  6192. * Due to various browser bugs, sometimes the Object implementation will be used even
  6193. * when the browser supports typed arrays.
  6194. *
  6195. * Note:
  6196. *
  6197. * - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances,
  6198. * See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
  6199. *
  6200. * - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
  6201. *
  6202. * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
  6203. * incorrect length in some situations.
  6204.  
  6205. * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they
  6206. * get the Object implementation, which is slower but behaves correctly.
  6207. */
  6208. Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined
  6209. ? global.TYPED_ARRAY_SUPPORT
  6210. : typedArraySupport()
  6211.  
  6212. /*
  6213. * Export kMaxLength after typed array support is determined.
  6214. */
  6215. exports.kMaxLength = kMaxLength()
  6216.  
  6217. function typedArraySupport () {
  6218. try {
  6219. var arr = new Uint8Array(1)
  6220. arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }}
  6221. return arr.foo() === 42 && // typed array instances can be augmented
  6222. typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray`
  6223. arr.subarray(1, 1).byteLength === 0 // ie10 has broken `subarray`
  6224. } catch (e) {
  6225. return false
  6226. }
  6227. }
  6228.  
  6229. function kMaxLength () {
  6230. return Buffer.TYPED_ARRAY_SUPPORT
  6231. ? 0x7fffffff
  6232. : 0x3fffffff
  6233. }
  6234.  
  6235. function createBuffer (that, length) {
  6236. if (kMaxLength() < length) {
  6237. throw new RangeError('Invalid typed array length')
  6238. }
  6239. if (Buffer.TYPED_ARRAY_SUPPORT) {
  6240. // Return an augmented `Uint8Array` instance, for best performance
  6241. that = new Uint8Array(length)
  6242. that.__proto__ = Buffer.prototype
  6243. } else {
  6244. // Fallback: Return an object instance of the Buffer class
  6245. if (that === null) {
  6246. that = new Buffer(length)
  6247. }
  6248. that.length = length
  6249. }
  6250.  
  6251. return that
  6252. }
  6253.  
  6254. /**
  6255. * The Buffer constructor returns instances of `Uint8Array` that have their
  6256. * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
  6257. * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
  6258. * and the `Uint8Array` methods. Square bracket notation works as expected -- it
  6259. * returns a single octet.
  6260. *
  6261. * The `Uint8Array` prototype remains unmodified.
  6262. */
  6263.  
  6264. function Buffer (arg, encodingOrOffset, length) {
  6265. if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
  6266. return new Buffer(arg, encodingOrOffset, length)
  6267. }
  6268.  
  6269. // Common case.
  6270. if (typeof arg === 'number') {
  6271. if (typeof encodingOrOffset === 'string') {
  6272. throw new Error(
  6273. 'If encoding is specified then the first argument must be a string'
  6274. )
  6275. }
  6276. return allocUnsafe(this, arg)
  6277. }
  6278. return from(this, arg, encodingOrOffset, length)
  6279. }
  6280.  
  6281. Buffer.poolSize = 8192 // not used by this implementation
  6282.  
  6283. // TODO: Legacy, not needed anymore. Remove in next major version.
  6284. Buffer._augment = function (arr) {
  6285. arr.__proto__ = Buffer.prototype
  6286. return arr
  6287. }
  6288.  
  6289. function from (that, value, encodingOrOffset, length) {
  6290. if (typeof value === 'number') {
  6291. throw new TypeError('"value" argument must not be a number')
  6292. }
  6293.  
  6294. if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
  6295. return fromArrayBuffer(that, value, encodingOrOffset, length)
  6296. }
  6297.  
  6298. if (typeof value === 'string') {
  6299. return fromString(that, value, encodingOrOffset)
  6300. }
  6301.  
  6302. return fromObject(that, value)
  6303. }
  6304.  
  6305. /**
  6306. * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
  6307. * if value is a number.
  6308. * Buffer.from(str[, encoding])
  6309. * Buffer.from(array)
  6310. * Buffer.from(buffer)
  6311. * Buffer.from(arrayBuffer[, byteOffset[, length]])
  6312. **/
  6313. Buffer.from = function (value, encodingOrOffset, length) {
  6314. return from(null, value, encodingOrOffset, length)
  6315. }
  6316.  
  6317. if (Buffer.TYPED_ARRAY_SUPPORT) {
  6318. Buffer.prototype.__proto__ = Uint8Array.prototype
  6319. Buffer.__proto__ = Uint8Array
  6320. if (typeof Symbol !== 'undefined' && Symbol.species &&
  6321. Buffer[Symbol.species] === Buffer) {
  6322. // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
  6323. Object.defineProperty(Buffer, Symbol.species, {
  6324. value: null,
  6325. configurable: true
  6326. })
  6327. }
  6328. }
  6329.  
  6330. function assertSize (size) {
  6331. if (typeof size !== 'number') {
  6332. throw new TypeError('"size" argument must be a number')
  6333. }
  6334. }
  6335.  
  6336. function alloc (that, size, fill, encoding) {
  6337. assertSize(size)
  6338. if (size <= 0) {
  6339. return createBuffer(that, size)
  6340. }
  6341. if (fill !== undefined) {
  6342. // Only pay attention to encoding if it's a string. This
  6343. // prevents accidentally sending in a number that would
  6344. // be interpretted as a start offset.
  6345. return typeof encoding === 'string'
  6346. ? createBuffer(that, size).fill(fill, encoding)
  6347. : createBuffer(that, size).fill(fill)
  6348. }
  6349. return createBuffer(that, size)
  6350. }
  6351.  
  6352. /**
  6353. * Creates a new filled Buffer instance.
  6354. * alloc(size[, fill[, encoding]])
  6355. **/
  6356. Buffer.alloc = function (size, fill, encoding) {
  6357. return alloc(null, size, fill, encoding)
  6358. }
  6359.  
  6360. function allocUnsafe (that, size) {
  6361. assertSize(size)
  6362. that = createBuffer(that, size < 0 ? 0 : checked(size) | 0)
  6363. if (!Buffer.TYPED_ARRAY_SUPPORT) {
  6364. for (var i = 0; i < size; ++i) {
  6365. that[i] = 0
  6366. }
  6367. }
  6368. return that
  6369. }
  6370.  
  6371. /**
  6372. * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
  6373. * */
  6374. Buffer.allocUnsafe = function (size) {
  6375. return allocUnsafe(null, size)
  6376. }
  6377. /**
  6378. * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
  6379. */
  6380. Buffer.allocUnsafeSlow = function (size) {
  6381. return allocUnsafe(null, size)
  6382. }
  6383.  
  6384. function fromString (that, string, encoding) {
  6385. if (typeof encoding !== 'string' || encoding === '') {
  6386. encoding = 'utf8'
  6387. }
  6388.  
  6389. if (!Buffer.isEncoding(encoding)) {
  6390. throw new TypeError('"encoding" must be a valid string encoding')
  6391. }
  6392.  
  6393. var length = byteLength(string, encoding) | 0
  6394. that = createBuffer(that, length)
  6395.  
  6396. var actual = that.write(string, encoding)
  6397.  
  6398. if (actual !== length) {
  6399. // Writing a hex string, for example, that contains invalid characters will
  6400. // cause everything after the first invalid character to be ignored. (e.g.
  6401. // 'abxxcd' will be treated as 'ab')
  6402. that = that.slice(0, actual)
  6403. }
  6404.  
  6405. return that
  6406. }
  6407.  
  6408. function fromArrayLike (that, array) {
  6409. var length = checked(array.length) | 0
  6410. that = createBuffer(that, length)
  6411. for (var i = 0; i < length; i += 1) {
  6412. that[i] = array[i] & 255
  6413. }
  6414. return that
  6415. }
  6416.  
  6417. function fromArrayBuffer (that, array, byteOffset, length) {
  6418. array.byteLength // this throws if `array` is not a valid ArrayBuffer
  6419.  
  6420. if (byteOffset < 0 || array.byteLength < byteOffset) {
  6421. throw new RangeError('\'offset\' is out of bounds')
  6422. }
  6423.  
  6424. if (array.byteLength < byteOffset + (length || 0)) {
  6425. throw new RangeError('\'length\' is out of bounds')
  6426. }
  6427.  
  6428. if (byteOffset === undefined && length === undefined) {
  6429. array = new Uint8Array(array)
  6430. } else if (length === undefined) {
  6431. array = new Uint8Array(array, byteOffset)
  6432. } else {
  6433. array = new Uint8Array(array, byteOffset, length)
  6434. }
  6435.  
  6436. if (Buffer.TYPED_ARRAY_SUPPORT) {
  6437. // Return an augmented `Uint8Array` instance, for best performance
  6438. that = array
  6439. that.__proto__ = Buffer.prototype
  6440. } else {
  6441. // Fallback: Return an object instance of the Buffer class
  6442. that = fromArrayLike(that, array)
  6443. }
  6444. return that
  6445. }
  6446.  
  6447. function fromObject (that, obj) {
  6448. if (Buffer.isBuffer(obj)) {
  6449. var len = checked(obj.length) | 0
  6450. that = createBuffer(that, len)
  6451.  
  6452. if (that.length === 0) {
  6453. return that
  6454. }
  6455.  
  6456. obj.copy(that, 0, 0, len)
  6457. return that
  6458. }
  6459.  
  6460. if (obj) {
  6461. if ((typeof ArrayBuffer !== 'undefined' &&
  6462. obj.buffer instanceof ArrayBuffer) || 'length' in obj) {
  6463. if (typeof obj.length !== 'number' || isnan(obj.length)) {
  6464. return createBuffer(that, 0)
  6465. }
  6466. return fromArrayLike(that, obj)
  6467. }
  6468.  
  6469. if (obj.type === 'Buffer' && isArray(obj.data)) {
  6470. return fromArrayLike(that, obj.data)
  6471. }
  6472. }
  6473.  
  6474. throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
  6475. }
  6476.  
  6477. function checked (length) {
  6478. // Note: cannot use `length < kMaxLength` here because that fails when
  6479. // length is NaN (which is otherwise coerced to zero.)
  6480. if (length >= kMaxLength()) {
  6481. throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
  6482. 'size: 0x' + kMaxLength().toString(16) + ' bytes')
  6483. }
  6484. return length | 0
  6485. }
  6486.  
  6487. function SlowBuffer (length) {
  6488. if (+length != length) { // eslint-disable-line eqeqeq
  6489. length = 0
  6490. }
  6491. return Buffer.alloc(+length)
  6492. }
  6493.  
  6494. Buffer.isBuffer = function isBuffer (b) {
  6495. return !!(b != null && b._isBuffer)
  6496. }
  6497.  
  6498. Buffer.compare = function compare (a, b) {
  6499. if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
  6500. throw new TypeError('Arguments must be Buffers')
  6501. }
  6502.  
  6503. if (a === b) return 0
  6504.  
  6505. var x = a.length
  6506. var y = b.length
  6507.  
  6508. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  6509. if (a[i] !== b[i]) {
  6510. x = a[i]
  6511. y = b[i]
  6512. break
  6513. }
  6514. }
  6515.  
  6516. if (x < y) return -1
  6517. if (y < x) return 1
  6518. return 0
  6519. }
  6520.  
  6521. Buffer.isEncoding = function isEncoding (encoding) {
  6522. switch (String(encoding).toLowerCase()) {
  6523. case 'hex':
  6524. case 'utf8':
  6525. case 'utf-8':
  6526. case 'ascii':
  6527. case 'latin1':
  6528. case 'binary':
  6529. case 'base64':
  6530. case 'ucs2':
  6531. case 'ucs-2':
  6532. case 'utf16le':
  6533. case 'utf-16le':
  6534. return true
  6535. default:
  6536. return false
  6537. }
  6538. }
  6539.  
  6540. Buffer.concat = function concat (list, length) {
  6541. if (!isArray(list)) {
  6542. throw new TypeError('"list" argument must be an Array of Buffers')
  6543. }
  6544.  
  6545. if (list.length === 0) {
  6546. return Buffer.alloc(0)
  6547. }
  6548.  
  6549. var i
  6550. if (length === undefined) {
  6551. length = 0
  6552. for (i = 0; i < list.length; ++i) {
  6553. length += list[i].length
  6554. }
  6555. }
  6556.  
  6557. var buffer = Buffer.allocUnsafe(length)
  6558. var pos = 0
  6559. for (i = 0; i < list.length; ++i) {
  6560. var buf = list[i]
  6561. if (!Buffer.isBuffer(buf)) {
  6562. throw new TypeError('"list" argument must be an Array of Buffers')
  6563. }
  6564. buf.copy(buffer, pos)
  6565. pos += buf.length
  6566. }
  6567. return buffer
  6568. }
  6569.  
  6570. function byteLength (string, encoding) {
  6571. if (Buffer.isBuffer(string)) {
  6572. return string.length
  6573. }
  6574. if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
  6575. (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
  6576. return string.byteLength
  6577. }
  6578. if (typeof string !== 'string') {
  6579. string = '' + string
  6580. }
  6581.  
  6582. var len = string.length
  6583. if (len === 0) return 0
  6584.  
  6585. // Use a for loop to avoid recursion
  6586. var loweredCase = false
  6587. for (;;) {
  6588. switch (encoding) {
  6589. case 'ascii':
  6590. case 'latin1':
  6591. case 'binary':
  6592. return len
  6593. case 'utf8':
  6594. case 'utf-8':
  6595. case undefined:
  6596. return utf8ToBytes(string).length
  6597. case 'ucs2':
  6598. case 'ucs-2':
  6599. case 'utf16le':
  6600. case 'utf-16le':
  6601. return len * 2
  6602. case 'hex':
  6603. return len >>> 1
  6604. case 'base64':
  6605. return base64ToBytes(string).length
  6606. default:
  6607. if (loweredCase) return utf8ToBytes(string).length // assume utf8
  6608. encoding = ('' + encoding).toLowerCase()
  6609. loweredCase = true
  6610. }
  6611. }
  6612. }
  6613. Buffer.byteLength = byteLength
  6614.  
  6615. function slowToString (encoding, start, end) {
  6616. var loweredCase = false
  6617.  
  6618. // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
  6619. // property of a typed array.
  6620.  
  6621. // This behaves neither like String nor Uint8Array in that we set start/end
  6622. // to their upper/lower bounds if the value passed is out of range.
  6623. // undefined is handled specially as per ECMA-262 6th Edition,
  6624. // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
  6625. if (start === undefined || start < 0) {
  6626. start = 0
  6627. }
  6628. // Return early if start > this.length. Done here to prevent potential uint32
  6629. // coercion fail below.
  6630. if (start > this.length) {
  6631. return ''
  6632. }
  6633.  
  6634. if (end === undefined || end > this.length) {
  6635. end = this.length
  6636. }
  6637.  
  6638. if (end <= 0) {
  6639. return ''
  6640. }
  6641.  
  6642. // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
  6643. end >>>= 0
  6644. start >>>= 0
  6645.  
  6646. if (end <= start) {
  6647. return ''
  6648. }
  6649.  
  6650. if (!encoding) encoding = 'utf8'
  6651.  
  6652. while (true) {
  6653. switch (encoding) {
  6654. case 'hex':
  6655. return hexSlice(this, start, end)
  6656.  
  6657. case 'utf8':
  6658. case 'utf-8':
  6659. return utf8Slice(this, start, end)
  6660.  
  6661. case 'ascii':
  6662. return asciiSlice(this, start, end)
  6663.  
  6664. case 'latin1':
  6665. case 'binary':
  6666. return latin1Slice(this, start, end)
  6667.  
  6668. case 'base64':
  6669. return base64Slice(this, start, end)
  6670.  
  6671. case 'ucs2':
  6672. case 'ucs-2':
  6673. case 'utf16le':
  6674. case 'utf-16le':
  6675. return utf16leSlice(this, start, end)
  6676.  
  6677. default:
  6678. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  6679. encoding = (encoding + '').toLowerCase()
  6680. loweredCase = true
  6681. }
  6682. }
  6683. }
  6684.  
  6685. // The property is used by `Buffer.isBuffer` and `is-buffer` (in Safari 5-7) to detect
  6686. // Buffer instances.
  6687. Buffer.prototype._isBuffer = true
  6688.  
  6689. function swap (b, n, m) {
  6690. var i = b[n]
  6691. b[n] = b[m]
  6692. b[m] = i
  6693. }
  6694.  
  6695. Buffer.prototype.swap16 = function swap16 () {
  6696. var len = this.length
  6697. if (len % 2 !== 0) {
  6698. throw new RangeError('Buffer size must be a multiple of 16-bits')
  6699. }
  6700. for (var i = 0; i < len; i += 2) {
  6701. swap(this, i, i + 1)
  6702. }
  6703. return this
  6704. }
  6705.  
  6706. Buffer.prototype.swap32 = function swap32 () {
  6707. var len = this.length
  6708. if (len % 4 !== 0) {
  6709. throw new RangeError('Buffer size must be a multiple of 32-bits')
  6710. }
  6711. for (var i = 0; i < len; i += 4) {
  6712. swap(this, i, i + 3)
  6713. swap(this, i + 1, i + 2)
  6714. }
  6715. return this
  6716. }
  6717.  
  6718. Buffer.prototype.swap64 = function swap64 () {
  6719. var len = this.length
  6720. if (len % 8 !== 0) {
  6721. throw new RangeError('Buffer size must be a multiple of 64-bits')
  6722. }
  6723. for (var i = 0; i < len; i += 8) {
  6724. swap(this, i, i + 7)
  6725. swap(this, i + 1, i + 6)
  6726. swap(this, i + 2, i + 5)
  6727. swap(this, i + 3, i + 4)
  6728. }
  6729. return this
  6730. }
  6731.  
  6732. Buffer.prototype.toString = function toString () {
  6733. var length = this.length | 0
  6734. if (length === 0) return ''
  6735. if (arguments.length === 0) return utf8Slice(this, 0, length)
  6736. return slowToString.apply(this, arguments)
  6737. }
  6738.  
  6739. Buffer.prototype.equals = function equals (b) {
  6740. if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
  6741. if (this === b) return true
  6742. return Buffer.compare(this, b) === 0
  6743. }
  6744.  
  6745. Buffer.prototype.inspect = function inspect () {
  6746. var str = ''
  6747. var max = exports.INSPECT_MAX_BYTES
  6748. if (this.length > 0) {
  6749. str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')
  6750. if (this.length > max) str += ' ... '
  6751. }
  6752. return '<Buffer ' + str + '>'
  6753. }
  6754.  
  6755. Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
  6756. if (!Buffer.isBuffer(target)) {
  6757. throw new TypeError('Argument must be a Buffer')
  6758. }
  6759.  
  6760. if (start === undefined) {
  6761. start = 0
  6762. }
  6763. if (end === undefined) {
  6764. end = target ? target.length : 0
  6765. }
  6766. if (thisStart === undefined) {
  6767. thisStart = 0
  6768. }
  6769. if (thisEnd === undefined) {
  6770. thisEnd = this.length
  6771. }
  6772.  
  6773. if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
  6774. throw new RangeError('out of range index')
  6775. }
  6776.  
  6777. if (thisStart >= thisEnd && start >= end) {
  6778. return 0
  6779. }
  6780. if (thisStart >= thisEnd) {
  6781. return -1
  6782. }
  6783. if (start >= end) {
  6784. return 1
  6785. }
  6786.  
  6787. start >>>= 0
  6788. end >>>= 0
  6789. thisStart >>>= 0
  6790. thisEnd >>>= 0
  6791.  
  6792. if (this === target) return 0
  6793.  
  6794. var x = thisEnd - thisStart
  6795. var y = end - start
  6796. var len = Math.min(x, y)
  6797.  
  6798. var thisCopy = this.slice(thisStart, thisEnd)
  6799. var targetCopy = target.slice(start, end)
  6800.  
  6801. for (var i = 0; i < len; ++i) {
  6802. if (thisCopy[i] !== targetCopy[i]) {
  6803. x = thisCopy[i]
  6804. y = targetCopy[i]
  6805. break
  6806. }
  6807. }
  6808.  
  6809. if (x < y) return -1
  6810. if (y < x) return 1
  6811. return 0
  6812. }
  6813.  
  6814. // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
  6815. // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
  6816. //
  6817. // Arguments:
  6818. // - buffer - a Buffer to search
  6819. // - val - a string, Buffer, or number
  6820. // - byteOffset - an index into `buffer`; will be clamped to an int32
  6821. // - encoding - an optional encoding, relevant is val is a string
  6822. // - dir - true for indexOf, false for lastIndexOf
  6823. function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
  6824. // Empty buffer means no match
  6825. if (buffer.length === 0) return -1
  6826.  
  6827. // Normalize byteOffset
  6828. if (typeof byteOffset === 'string') {
  6829. encoding = byteOffset
  6830. byteOffset = 0
  6831. } else if (byteOffset > 0x7fffffff) {
  6832. byteOffset = 0x7fffffff
  6833. } else if (byteOffset < -0x80000000) {
  6834. byteOffset = -0x80000000
  6835. }
  6836. byteOffset = +byteOffset // Coerce to Number.
  6837. if (isNaN(byteOffset)) {
  6838. // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
  6839. byteOffset = dir ? 0 : (buffer.length - 1)
  6840. }
  6841.  
  6842. // Normalize byteOffset: negative offsets start from the end of the buffer
  6843. if (byteOffset < 0) byteOffset = buffer.length + byteOffset
  6844. if (byteOffset >= buffer.length) {
  6845. if (dir) return -1
  6846. else byteOffset = buffer.length - 1
  6847. } else if (byteOffset < 0) {
  6848. if (dir) byteOffset = 0
  6849. else return -1
  6850. }
  6851.  
  6852. // Normalize val
  6853. if (typeof val === 'string') {
  6854. val = Buffer.from(val, encoding)
  6855. }
  6856.  
  6857. // Finally, search either indexOf (if dir is true) or lastIndexOf
  6858. if (Buffer.isBuffer(val)) {
  6859. // Special case: looking for empty string/buffer always fails
  6860. if (val.length === 0) {
  6861. return -1
  6862. }
  6863. return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
  6864. } else if (typeof val === 'number') {
  6865. val = val & 0xFF // Search for a byte value [0-255]
  6866. if (Buffer.TYPED_ARRAY_SUPPORT &&
  6867. typeof Uint8Array.prototype.indexOf === 'function') {
  6868. if (dir) {
  6869. return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
  6870. } else {
  6871. return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
  6872. }
  6873. }
  6874. return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)
  6875. }
  6876.  
  6877. throw new TypeError('val must be string, number or Buffer')
  6878. }
  6879.  
  6880. function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
  6881. var indexSize = 1
  6882. var arrLength = arr.length
  6883. var valLength = val.length
  6884.  
  6885. if (encoding !== undefined) {
  6886. encoding = String(encoding).toLowerCase()
  6887. if (encoding === 'ucs2' || encoding === 'ucs-2' ||
  6888. encoding === 'utf16le' || encoding === 'utf-16le') {
  6889. if (arr.length < 2 || val.length < 2) {
  6890. return -1
  6891. }
  6892. indexSize = 2
  6893. arrLength /= 2
  6894. valLength /= 2
  6895. byteOffset /= 2
  6896. }
  6897. }
  6898.  
  6899. function read (buf, i) {
  6900. if (indexSize === 1) {
  6901. return buf[i]
  6902. } else {
  6903. return buf.readUInt16BE(i * indexSize)
  6904. }
  6905. }
  6906.  
  6907. var i
  6908. if (dir) {
  6909. var foundIndex = -1
  6910. for (i = byteOffset; i < arrLength; i++) {
  6911. if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
  6912. if (foundIndex === -1) foundIndex = i
  6913. if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
  6914. } else {
  6915. if (foundIndex !== -1) i -= i - foundIndex
  6916. foundIndex = -1
  6917. }
  6918. }
  6919. } else {
  6920. if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
  6921. for (i = byteOffset; i >= 0; i--) {
  6922. var found = true
  6923. for (var j = 0; j < valLength; j++) {
  6924. if (read(arr, i + j) !== read(val, j)) {
  6925. found = false
  6926. break
  6927. }
  6928. }
  6929. if (found) return i
  6930. }
  6931. }
  6932.  
  6933. return -1
  6934. }
  6935.  
  6936. Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
  6937. return this.indexOf(val, byteOffset, encoding) !== -1
  6938. }
  6939.  
  6940. Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
  6941. return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
  6942. }
  6943.  
  6944. Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
  6945. return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
  6946. }
  6947.  
  6948. function hexWrite (buf, string, offset, length) {
  6949. offset = Number(offset) || 0
  6950. var remaining = buf.length - offset
  6951. if (!length) {
  6952. length = remaining
  6953. } else {
  6954. length = Number(length)
  6955. if (length > remaining) {
  6956. length = remaining
  6957. }
  6958. }
  6959.  
  6960. // must be an even number of digits
  6961. var strLen = string.length
  6962. if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')
  6963.  
  6964. if (length > strLen / 2) {
  6965. length = strLen / 2
  6966. }
  6967. for (var i = 0; i < length; ++i) {
  6968. var parsed = parseInt(string.substr(i * 2, 2), 16)
  6969. if (isNaN(parsed)) return i
  6970. buf[offset + i] = parsed
  6971. }
  6972. return i
  6973. }
  6974.  
  6975. function utf8Write (buf, string, offset, length) {
  6976. return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
  6977. }
  6978.  
  6979. function asciiWrite (buf, string, offset, length) {
  6980. return blitBuffer(asciiToBytes(string), buf, offset, length)
  6981. }
  6982.  
  6983. function latin1Write (buf, string, offset, length) {
  6984. return asciiWrite(buf, string, offset, length)
  6985. }
  6986.  
  6987. function base64Write (buf, string, offset, length) {
  6988. return blitBuffer(base64ToBytes(string), buf, offset, length)
  6989. }
  6990.  
  6991. function ucs2Write (buf, string, offset, length) {
  6992. return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
  6993. }
  6994.  
  6995. Buffer.prototype.write = function write (string, offset, length, encoding) {
  6996. // Buffer#write(string)
  6997. if (offset === undefined) {
  6998. encoding = 'utf8'
  6999. length = this.length
  7000. offset = 0
  7001. // Buffer#write(string, encoding)
  7002. } else if (length === undefined && typeof offset === 'string') {
  7003. encoding = offset
  7004. length = this.length
  7005. offset = 0
  7006. // Buffer#write(string, offset[, length][, encoding])
  7007. } else if (isFinite(offset)) {
  7008. offset = offset | 0
  7009. if (isFinite(length)) {
  7010. length = length | 0
  7011. if (encoding === undefined) encoding = 'utf8'
  7012. } else {
  7013. encoding = length
  7014. length = undefined
  7015. }
  7016. // legacy write(string, encoding, offset, length) - remove in v0.13
  7017. } else {
  7018. throw new Error(
  7019. 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
  7020. )
  7021. }
  7022.  
  7023. var remaining = this.length - offset
  7024. if (length === undefined || length > remaining) length = remaining
  7025.  
  7026. if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
  7027. throw new RangeError('Attempt to write outside buffer bounds')
  7028. }
  7029.  
  7030. if (!encoding) encoding = 'utf8'
  7031.  
  7032. var loweredCase = false
  7033. for (;;) {
  7034. switch (encoding) {
  7035. case 'hex':
  7036. return hexWrite(this, string, offset, length)
  7037.  
  7038. case 'utf8':
  7039. case 'utf-8':
  7040. return utf8Write(this, string, offset, length)
  7041.  
  7042. case 'ascii':
  7043. return asciiWrite(this, string, offset, length)
  7044.  
  7045. case 'latin1':
  7046. case 'binary':
  7047. return latin1Write(this, string, offset, length)
  7048.  
  7049. case 'base64':
  7050. // Warning: maxLength not taken into account in base64Write
  7051. return base64Write(this, string, offset, length)
  7052.  
  7053. case 'ucs2':
  7054. case 'ucs-2':
  7055. case 'utf16le':
  7056. case 'utf-16le':
  7057. return ucs2Write(this, string, offset, length)
  7058.  
  7059. default:
  7060. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  7061. encoding = ('' + encoding).toLowerCase()
  7062. loweredCase = true
  7063. }
  7064. }
  7065. }
  7066.  
  7067. Buffer.prototype.toJSON = function toJSON () {
  7068. return {
  7069. type: 'Buffer',
  7070. data: Array.prototype.slice.call(this._arr || this, 0)
  7071. }
  7072. }
  7073.  
  7074. function base64Slice (buf, start, end) {
  7075. if (start === 0 && end === buf.length) {
  7076. return base64.fromByteArray(buf)
  7077. } else {
  7078. return base64.fromByteArray(buf.slice(start, end))
  7079. }
  7080. }
  7081.  
  7082. function utf8Slice (buf, start, end) {
  7083. end = Math.min(buf.length, end)
  7084. var res = []
  7085.  
  7086. var i = start
  7087. while (i < end) {
  7088. var firstByte = buf[i]
  7089. var codePoint = null
  7090. var bytesPerSequence = (firstByte > 0xEF) ? 4
  7091. : (firstByte > 0xDF) ? 3
  7092. : (firstByte > 0xBF) ? 2
  7093. : 1
  7094.  
  7095. if (i + bytesPerSequence <= end) {
  7096. var secondByte, thirdByte, fourthByte, tempCodePoint
  7097.  
  7098. switch (bytesPerSequence) {
  7099. case 1:
  7100. if (firstByte < 0x80) {
  7101. codePoint = firstByte
  7102. }
  7103. break
  7104. case 2:
  7105. secondByte = buf[i + 1]
  7106. if ((secondByte & 0xC0) === 0x80) {
  7107. tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
  7108. if (tempCodePoint > 0x7F) {
  7109. codePoint = tempCodePoint
  7110. }
  7111. }
  7112. break
  7113. case 3:
  7114. secondByte = buf[i + 1]
  7115. thirdByte = buf[i + 2]
  7116. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
  7117. tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
  7118. if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
  7119. codePoint = tempCodePoint
  7120. }
  7121. }
  7122. break
  7123. case 4:
  7124. secondByte = buf[i + 1]
  7125. thirdByte = buf[i + 2]
  7126. fourthByte = buf[i + 3]
  7127. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
  7128. tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
  7129. if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
  7130. codePoint = tempCodePoint
  7131. }
  7132. }
  7133. }
  7134. }
  7135.  
  7136. if (codePoint === null) {
  7137. // we did not generate a valid codePoint so insert a
  7138. // replacement char (U+FFFD) and advance only 1 byte
  7139. codePoint = 0xFFFD
  7140. bytesPerSequence = 1
  7141. } else if (codePoint > 0xFFFF) {
  7142. // encode to utf16 (surrogate pair dance)
  7143. codePoint -= 0x10000
  7144. res.push(codePoint >>> 10 & 0x3FF | 0xD800)
  7145. codePoint = 0xDC00 | codePoint & 0x3FF
  7146. }
  7147.  
  7148. res.push(codePoint)
  7149. i += bytesPerSequence
  7150. }
  7151.  
  7152. return decodeCodePointsArray(res)
  7153. }
  7154.  
  7155. // Based on http://stackoverflow.com/a/22747272/680742, the browser with
  7156. // the lowest limit is Chrome, with 0x10000 args.
  7157. // We go 1 magnitude less, for safety
  7158. var MAX_ARGUMENTS_LENGTH = 0x1000
  7159.  
  7160. function decodeCodePointsArray (codePoints) {
  7161. var len = codePoints.length
  7162. if (len <= MAX_ARGUMENTS_LENGTH) {
  7163. return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
  7164. }
  7165.  
  7166. // Decode in chunks to avoid "call stack size exceeded".
  7167. var res = ''
  7168. var i = 0
  7169. while (i < len) {
  7170. res += String.fromCharCode.apply(
  7171. String,
  7172. codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
  7173. )
  7174. }
  7175. return res
  7176. }
  7177.  
  7178. function asciiSlice (buf, start, end) {
  7179. var ret = ''
  7180. end = Math.min(buf.length, end)
  7181.  
  7182. for (var i = start; i < end; ++i) {
  7183. ret += String.fromCharCode(buf[i] & 0x7F)
  7184. }
  7185. return ret
  7186. }
  7187.  
  7188. function latin1Slice (buf, start, end) {
  7189. var ret = ''
  7190. end = Math.min(buf.length, end)
  7191.  
  7192. for (var i = start; i < end; ++i) {
  7193. ret += String.fromCharCode(buf[i])
  7194. }
  7195. return ret
  7196. }
  7197.  
  7198. function hexSlice (buf, start, end) {
  7199. var len = buf.length
  7200.  
  7201. if (!start || start < 0) start = 0
  7202. if (!end || end < 0 || end > len) end = len
  7203.  
  7204. var out = ''
  7205. for (var i = start; i < end; ++i) {
  7206. out += toHex(buf[i])
  7207. }
  7208. return out
  7209. }
  7210.  
  7211. function utf16leSlice (buf, start, end) {
  7212. var bytes = buf.slice(start, end)
  7213. var res = ''
  7214. for (var i = 0; i < bytes.length; i += 2) {
  7215. res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256)
  7216. }
  7217. return res
  7218. }
  7219.  
  7220. Buffer.prototype.slice = function slice (start, end) {
  7221. var len = this.length
  7222. start = ~~start
  7223. end = end === undefined ? len : ~~end
  7224.  
  7225. if (start < 0) {
  7226. start += len
  7227. if (start < 0) start = 0
  7228. } else if (start > len) {
  7229. start = len
  7230. }
  7231.  
  7232. if (end < 0) {
  7233. end += len
  7234. if (end < 0) end = 0
  7235. } else if (end > len) {
  7236. end = len
  7237. }
  7238.  
  7239. if (end < start) end = start
  7240.  
  7241. var newBuf
  7242. if (Buffer.TYPED_ARRAY_SUPPORT) {
  7243. newBuf = this.subarray(start, end)
  7244. newBuf.__proto__ = Buffer.prototype
  7245. } else {
  7246. var sliceLen = end - start
  7247. newBuf = new Buffer(sliceLen, undefined)
  7248. for (var i = 0; i < sliceLen; ++i) {
  7249. newBuf[i] = this[i + start]
  7250. }
  7251. }
  7252.  
  7253. return newBuf
  7254. }
  7255.  
  7256. /*
  7257. * Need to make sure that buffer isn't trying to write out of bounds.
  7258. */
  7259. function checkOffset (offset, ext, length) {
  7260. if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
  7261. if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
  7262. }
  7263.  
  7264. Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
  7265. offset = offset | 0
  7266. byteLength = byteLength | 0
  7267. if (!noAssert) checkOffset(offset, byteLength, this.length)
  7268.  
  7269. var val = this[offset]
  7270. var mul = 1
  7271. var i = 0
  7272. while (++i < byteLength && (mul *= 0x100)) {
  7273. val += this[offset + i] * mul
  7274. }
  7275.  
  7276. return val
  7277. }
  7278.  
  7279. Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
  7280. offset = offset | 0
  7281. byteLength = byteLength | 0
  7282. if (!noAssert) {
  7283. checkOffset(offset, byteLength, this.length)
  7284. }
  7285.  
  7286. var val = this[offset + --byteLength]
  7287. var mul = 1
  7288. while (byteLength > 0 && (mul *= 0x100)) {
  7289. val += this[offset + --byteLength] * mul
  7290. }
  7291.  
  7292. return val
  7293. }
  7294.  
  7295. Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
  7296. if (!noAssert) checkOffset(offset, 1, this.length)
  7297. return this[offset]
  7298. }
  7299.  
  7300. Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
  7301. if (!noAssert) checkOffset(offset, 2, this.length)
  7302. return this[offset] | (this[offset + 1] << 8)
  7303. }
  7304.  
  7305. Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
  7306. if (!noAssert) checkOffset(offset, 2, this.length)
  7307. return (this[offset] << 8) | this[offset + 1]
  7308. }
  7309.  
  7310. Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
  7311. if (!noAssert) checkOffset(offset, 4, this.length)
  7312.  
  7313. return ((this[offset]) |
  7314. (this[offset + 1] << 8) |
  7315. (this[offset + 2] << 16)) +
  7316. (this[offset + 3] * 0x1000000)
  7317. }
  7318.  
  7319. Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
  7320. if (!noAssert) checkOffset(offset, 4, this.length)
  7321.  
  7322. return (this[offset] * 0x1000000) +
  7323. ((this[offset + 1] << 16) |
  7324. (this[offset + 2] << 8) |
  7325. this[offset + 3])
  7326. }
  7327.  
  7328. Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
  7329. offset = offset | 0
  7330. byteLength = byteLength | 0
  7331. if (!noAssert) checkOffset(offset, byteLength, this.length)
  7332.  
  7333. var val = this[offset]
  7334. var mul = 1
  7335. var i = 0
  7336. while (++i < byteLength && (mul *= 0x100)) {
  7337. val += this[offset + i] * mul
  7338. }
  7339. mul *= 0x80
  7340.  
  7341. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  7342.  
  7343. return val
  7344. }
  7345.  
  7346. Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
  7347. offset = offset | 0
  7348. byteLength = byteLength | 0
  7349. if (!noAssert) checkOffset(offset, byteLength, this.length)
  7350.  
  7351. var i = byteLength
  7352. var mul = 1
  7353. var val = this[offset + --i]
  7354. while (i > 0 && (mul *= 0x100)) {
  7355. val += this[offset + --i] * mul
  7356. }
  7357. mul *= 0x80
  7358.  
  7359. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  7360.  
  7361. return val
  7362. }
  7363.  
  7364. Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
  7365. if (!noAssert) checkOffset(offset, 1, this.length)
  7366. if (!(this[offset] & 0x80)) return (this[offset])
  7367. return ((0xff - this[offset] + 1) * -1)
  7368. }
  7369.  
  7370. Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
  7371. if (!noAssert) checkOffset(offset, 2, this.length)
  7372. var val = this[offset] | (this[offset + 1] << 8)
  7373. return (val & 0x8000) ? val | 0xFFFF0000 : val
  7374. }
  7375.  
  7376. Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
  7377. if (!noAssert) checkOffset(offset, 2, this.length)
  7378. var val = this[offset + 1] | (this[offset] << 8)
  7379. return (val & 0x8000) ? val | 0xFFFF0000 : val
  7380. }
  7381.  
  7382. Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
  7383. if (!noAssert) checkOffset(offset, 4, this.length)
  7384.  
  7385. return (this[offset]) |
  7386. (this[offset + 1] << 8) |
  7387. (this[offset + 2] << 16) |
  7388. (this[offset + 3] << 24)
  7389. }
  7390.  
  7391. Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
  7392. if (!noAssert) checkOffset(offset, 4, this.length)
  7393.  
  7394. return (this[offset] << 24) |
  7395. (this[offset + 1] << 16) |
  7396. (this[offset + 2] << 8) |
  7397. (this[offset + 3])
  7398. }
  7399.  
  7400. Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
  7401. if (!noAssert) checkOffset(offset, 4, this.length)
  7402. return ieee754.read(this, offset, true, 23, 4)
  7403. }
  7404.  
  7405. Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
  7406. if (!noAssert) checkOffset(offset, 4, this.length)
  7407. return ieee754.read(this, offset, false, 23, 4)
  7408. }
  7409.  
  7410. Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
  7411. if (!noAssert) checkOffset(offset, 8, this.length)
  7412. return ieee754.read(this, offset, true, 52, 8)
  7413. }
  7414.  
  7415. Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
  7416. if (!noAssert) checkOffset(offset, 8, this.length)
  7417. return ieee754.read(this, offset, false, 52, 8)
  7418. }
  7419.  
  7420. function checkInt (buf, value, offset, ext, max, min) {
  7421. if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
  7422. if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
  7423. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  7424. }
  7425.  
  7426. Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
  7427. value = +value
  7428. offset = offset | 0
  7429. byteLength = byteLength | 0
  7430. if (!noAssert) {
  7431. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  7432. checkInt(this, value, offset, byteLength, maxBytes, 0)
  7433. }
  7434.  
  7435. var mul = 1
  7436. var i = 0
  7437. this[offset] = value & 0xFF
  7438. while (++i < byteLength && (mul *= 0x100)) {
  7439. this[offset + i] = (value / mul) & 0xFF
  7440. }
  7441.  
  7442. return offset + byteLength
  7443. }
  7444.  
  7445. Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
  7446. value = +value
  7447. offset = offset | 0
  7448. byteLength = byteLength | 0
  7449. if (!noAssert) {
  7450. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  7451. checkInt(this, value, offset, byteLength, maxBytes, 0)
  7452. }
  7453.  
  7454. var i = byteLength - 1
  7455. var mul = 1
  7456. this[offset + i] = value & 0xFF
  7457. while (--i >= 0 && (mul *= 0x100)) {
  7458. this[offset + i] = (value / mul) & 0xFF
  7459. }
  7460.  
  7461. return offset + byteLength
  7462. }
  7463.  
  7464. Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
  7465. value = +value
  7466. offset = offset | 0
  7467. if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
  7468. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
  7469. this[offset] = (value & 0xff)
  7470. return offset + 1
  7471. }
  7472.  
  7473. function objectWriteUInt16 (buf, value, offset, littleEndian) {
  7474. if (value < 0) value = 0xffff + value + 1
  7475. for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; ++i) {
  7476. buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
  7477. (littleEndian ? i : 1 - i) * 8
  7478. }
  7479. }
  7480.  
  7481. Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
  7482. value = +value
  7483. offset = offset | 0
  7484. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  7485. if (Buffer.TYPED_ARRAY_SUPPORT) {
  7486. this[offset] = (value & 0xff)
  7487. this[offset + 1] = (value >>> 8)
  7488. } else {
  7489. objectWriteUInt16(this, value, offset, true)
  7490. }
  7491. return offset + 2
  7492. }
  7493.  
  7494. Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
  7495. value = +value
  7496. offset = offset | 0
  7497. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  7498. if (Buffer.TYPED_ARRAY_SUPPORT) {
  7499. this[offset] = (value >>> 8)
  7500. this[offset + 1] = (value & 0xff)
  7501. } else {
  7502. objectWriteUInt16(this, value, offset, false)
  7503. }
  7504. return offset + 2
  7505. }
  7506.  
  7507. function objectWriteUInt32 (buf, value, offset, littleEndian) {
  7508. if (value < 0) value = 0xffffffff + value + 1
  7509. for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; ++i) {
  7510. buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
  7511. }
  7512. }
  7513.  
  7514. Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
  7515. value = +value
  7516. offset = offset | 0
  7517. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  7518. if (Buffer.TYPED_ARRAY_SUPPORT) {
  7519. this[offset + 3] = (value >>> 24)
  7520. this[offset + 2] = (value >>> 16)
  7521. this[offset + 1] = (value >>> 8)
  7522. this[offset] = (value & 0xff)
  7523. } else {
  7524. objectWriteUInt32(this, value, offset, true)
  7525. }
  7526. return offset + 4
  7527. }
  7528.  
  7529. Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
  7530. value = +value
  7531. offset = offset | 0
  7532. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  7533. if (Buffer.TYPED_ARRAY_SUPPORT) {
  7534. this[offset] = (value >>> 24)
  7535. this[offset + 1] = (value >>> 16)
  7536. this[offset + 2] = (value >>> 8)
  7537. this[offset + 3] = (value & 0xff)
  7538. } else {
  7539. objectWriteUInt32(this, value, offset, false)
  7540. }
  7541. return offset + 4
  7542. }
  7543.  
  7544. Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
  7545. value = +value
  7546. offset = offset | 0
  7547. if (!noAssert) {
  7548. var limit = Math.pow(2, 8 * byteLength - 1)
  7549.  
  7550. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  7551. }
  7552.  
  7553. var i = 0
  7554. var mul = 1
  7555. var sub = 0
  7556. this[offset] = value & 0xFF
  7557. while (++i < byteLength && (mul *= 0x100)) {
  7558. if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
  7559. sub = 1
  7560. }
  7561. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  7562. }
  7563.  
  7564. return offset + byteLength
  7565. }
  7566.  
  7567. Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
  7568. value = +value
  7569. offset = offset | 0
  7570. if (!noAssert) {
  7571. var limit = Math.pow(2, 8 * byteLength - 1)
  7572.  
  7573. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  7574. }
  7575.  
  7576. var i = byteLength - 1
  7577. var mul = 1
  7578. var sub = 0
  7579. this[offset + i] = value & 0xFF
  7580. while (--i >= 0 && (mul *= 0x100)) {
  7581. if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
  7582. sub = 1
  7583. }
  7584. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  7585. }
  7586.  
  7587. return offset + byteLength
  7588. }
  7589.  
  7590. Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
  7591. value = +value
  7592. offset = offset | 0
  7593. if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
  7594. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
  7595. if (value < 0) value = 0xff + value + 1
  7596. this[offset] = (value & 0xff)
  7597. return offset + 1
  7598. }
  7599.  
  7600. Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
  7601. value = +value
  7602. offset = offset | 0
  7603. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  7604. if (Buffer.TYPED_ARRAY_SUPPORT) {
  7605. this[offset] = (value & 0xff)
  7606. this[offset + 1] = (value >>> 8)
  7607. } else {
  7608. objectWriteUInt16(this, value, offset, true)
  7609. }
  7610. return offset + 2
  7611. }
  7612.  
  7613. Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
  7614. value = +value
  7615. offset = offset | 0
  7616. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  7617. if (Buffer.TYPED_ARRAY_SUPPORT) {
  7618. this[offset] = (value >>> 8)
  7619. this[offset + 1] = (value & 0xff)
  7620. } else {
  7621. objectWriteUInt16(this, value, offset, false)
  7622. }
  7623. return offset + 2
  7624. }
  7625.  
  7626. Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
  7627. value = +value
  7628. offset = offset | 0
  7629. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  7630. if (Buffer.TYPED_ARRAY_SUPPORT) {
  7631. this[offset] = (value & 0xff)
  7632. this[offset + 1] = (value >>> 8)
  7633. this[offset + 2] = (value >>> 16)
  7634. this[offset + 3] = (value >>> 24)
  7635. } else {
  7636. objectWriteUInt32(this, value, offset, true)
  7637. }
  7638. return offset + 4
  7639. }
  7640.  
  7641. Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
  7642. value = +value
  7643. offset = offset | 0
  7644. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  7645. if (value < 0) value = 0xffffffff + value + 1
  7646. if (Buffer.TYPED_ARRAY_SUPPORT) {
  7647. this[offset] = (value >>> 24)
  7648. this[offset + 1] = (value >>> 16)
  7649. this[offset + 2] = (value >>> 8)
  7650. this[offset + 3] = (value & 0xff)
  7651. } else {
  7652. objectWriteUInt32(this, value, offset, false)
  7653. }
  7654. return offset + 4
  7655. }
  7656.  
  7657. function checkIEEE754 (buf, value, offset, ext, max, min) {
  7658. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  7659. if (offset < 0) throw new RangeError('Index out of range')
  7660. }
  7661.  
  7662. function writeFloat (buf, value, offset, littleEndian, noAssert) {
  7663. if (!noAssert) {
  7664. checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
  7665. }
  7666. ieee754.write(buf, value, offset, littleEndian, 23, 4)
  7667. return offset + 4
  7668. }
  7669.  
  7670. Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
  7671. return writeFloat(this, value, offset, true, noAssert)
  7672. }
  7673.  
  7674. Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
  7675. return writeFloat(this, value, offset, false, noAssert)
  7676. }
  7677.  
  7678. function writeDouble (buf, value, offset, littleEndian, noAssert) {
  7679. if (!noAssert) {
  7680. checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
  7681. }
  7682. ieee754.write(buf, value, offset, littleEndian, 52, 8)
  7683. return offset + 8
  7684. }
  7685.  
  7686. Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
  7687. return writeDouble(this, value, offset, true, noAssert)
  7688. }
  7689.  
  7690. Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
  7691. return writeDouble(this, value, offset, false, noAssert)
  7692. }
  7693.  
  7694. // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
  7695. Buffer.prototype.copy = function copy (target, targetStart, start, end) {
  7696. if (!start) start = 0
  7697. if (!end && end !== 0) end = this.length
  7698. if (targetStart >= target.length) targetStart = target.length
  7699. if (!targetStart) targetStart = 0
  7700. if (end > 0 && end < start) end = start
  7701.  
  7702. // Copy 0 bytes; we're done
  7703. if (end === start) return 0
  7704. if (target.length === 0 || this.length === 0) return 0
  7705.  
  7706. // Fatal error conditions
  7707. if (targetStart < 0) {
  7708. throw new RangeError('targetStart out of bounds')
  7709. }
  7710. if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
  7711. if (end < 0) throw new RangeError('sourceEnd out of bounds')
  7712.  
  7713. // Are we oob?
  7714. if (end > this.length) end = this.length
  7715. if (target.length - targetStart < end - start) {
  7716. end = target.length - targetStart + start
  7717. }
  7718.  
  7719. var len = end - start
  7720. var i
  7721.  
  7722. if (this === target && start < targetStart && targetStart < end) {
  7723. // descending copy from end
  7724. for (i = len - 1; i >= 0; --i) {
  7725. target[i + targetStart] = this[i + start]
  7726. }
  7727. } else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
  7728. // ascending copy from start
  7729. for (i = 0; i < len; ++i) {
  7730. target[i + targetStart] = this[i + start]
  7731. }
  7732. } else {
  7733. Uint8Array.prototype.set.call(
  7734. target,
  7735. this.subarray(start, start + len),
  7736. targetStart
  7737. )
  7738. }
  7739.  
  7740. return len
  7741. }
  7742.  
  7743. // Usage:
  7744. // buffer.fill(number[, offset[, end]])
  7745. // buffer.fill(buffer[, offset[, end]])
  7746. // buffer.fill(string[, offset[, end]][, encoding])
  7747. Buffer.prototype.fill = function fill (val, start, end, encoding) {
  7748. // Handle string cases:
  7749. if (typeof val === 'string') {
  7750. if (typeof start === 'string') {
  7751. encoding = start
  7752. start = 0
  7753. end = this.length
  7754. } else if (typeof end === 'string') {
  7755. encoding = end
  7756. end = this.length
  7757. }
  7758. if (val.length === 1) {
  7759. var code = val.charCodeAt(0)
  7760. if (code < 256) {
  7761. val = code
  7762. }
  7763. }
  7764. if (encoding !== undefined && typeof encoding !== 'string') {
  7765. throw new TypeError('encoding must be a string')
  7766. }
  7767. if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
  7768. throw new TypeError('Unknown encoding: ' + encoding)
  7769. }
  7770. } else if (typeof val === 'number') {
  7771. val = val & 255
  7772. }
  7773.  
  7774. // Invalid ranges are not set to a default, so can range check early.
  7775. if (start < 0 || this.length < start || this.length < end) {
  7776. throw new RangeError('Out of range index')
  7777. }
  7778.  
  7779. if (end <= start) {
  7780. return this
  7781. }
  7782.  
  7783. start = start >>> 0
  7784. end = end === undefined ? this.length : end >>> 0
  7785.  
  7786. if (!val) val = 0
  7787.  
  7788. var i
  7789. if (typeof val === 'number') {
  7790. for (i = start; i < end; ++i) {
  7791. this[i] = val
  7792. }
  7793. } else {
  7794. var bytes = Buffer.isBuffer(val)
  7795. ? val
  7796. : utf8ToBytes(new Buffer(val, encoding).toString())
  7797. var len = bytes.length
  7798. for (i = 0; i < end - start; ++i) {
  7799. this[i + start] = bytes[i % len]
  7800. }
  7801. }
  7802.  
  7803. return this
  7804. }
  7805.  
  7806. // HELPER FUNCTIONS
  7807. // ================
  7808.  
  7809. var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g
  7810.  
  7811. function base64clean (str) {
  7812. // Node strips out invalid characters like \n and \t from the string, base64-js does not
  7813. str = stringtrim(str).replace(INVALID_BASE64_RE, '')
  7814. // Node converts strings with length < 2 to ''
  7815. if (str.length < 2) return ''
  7816. // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
  7817. while (str.length % 4 !== 0) {
  7818. str = str + '='
  7819. }
  7820. return str
  7821. }
  7822.  
  7823. function stringtrim (str) {
  7824. if (str.trim) return str.trim()
  7825. return str.replace(/^\s+|\s+$/g, '')
  7826. }
  7827.  
  7828. function toHex (n) {
  7829. if (n < 16) return '0' + n.toString(16)
  7830. return n.toString(16)
  7831. }
  7832.  
  7833. function utf8ToBytes (string, units) {
  7834. units = units || Infinity
  7835. var codePoint
  7836. var length = string.length
  7837. var leadSurrogate = null
  7838. var bytes = []
  7839.  
  7840. for (var i = 0; i < length; ++i) {
  7841. codePoint = string.charCodeAt(i)
  7842.  
  7843. // is surrogate component
  7844. if (codePoint > 0xD7FF && codePoint < 0xE000) {
  7845. // last char was a lead
  7846. if (!leadSurrogate) {
  7847. // no lead yet
  7848. if (codePoint > 0xDBFF) {
  7849. // unexpected trail
  7850. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  7851. continue
  7852. } else if (i + 1 === length) {
  7853. // unpaired lead
  7854. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  7855. continue
  7856. }
  7857.  
  7858. // valid lead
  7859. leadSurrogate = codePoint
  7860.  
  7861. continue
  7862. }
  7863.  
  7864. // 2 leads in a row
  7865. if (codePoint < 0xDC00) {
  7866. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  7867. leadSurrogate = codePoint
  7868. continue
  7869. }
  7870.  
  7871. // valid surrogate pair
  7872. codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
  7873. } else if (leadSurrogate) {
  7874. // valid bmp char, but last char was a lead
  7875. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  7876. }
  7877.  
  7878. leadSurrogate = null
  7879.  
  7880. // encode utf8
  7881. if (codePoint < 0x80) {
  7882. if ((units -= 1) < 0) break
  7883. bytes.push(codePoint)
  7884. } else if (codePoint < 0x800) {
  7885. if ((units -= 2) < 0) break
  7886. bytes.push(
  7887. codePoint >> 0x6 | 0xC0,
  7888. codePoint & 0x3F | 0x80
  7889. )
  7890. } else if (codePoint < 0x10000) {
  7891. if ((units -= 3) < 0) break
  7892. bytes.push(
  7893. codePoint >> 0xC | 0xE0,
  7894. codePoint >> 0x6 & 0x3F | 0x80,
  7895. codePoint & 0x3F | 0x80
  7896. )
  7897. } else if (codePoint < 0x110000) {
  7898. if ((units -= 4) < 0) break
  7899. bytes.push(
  7900. codePoint >> 0x12 | 0xF0,
  7901. codePoint >> 0xC & 0x3F | 0x80,
  7902. codePoint >> 0x6 & 0x3F | 0x80,
  7903. codePoint & 0x3F | 0x80
  7904. )
  7905. } else {
  7906. throw new Error('Invalid code point')
  7907. }
  7908. }
  7909.  
  7910. return bytes
  7911. }
  7912.  
  7913. function asciiToBytes (str) {
  7914. var byteArray = []
  7915. for (var i = 0; i < str.length; ++i) {
  7916. // Node's code seems to be doing this and not & 0x7F..
  7917. byteArray.push(str.charCodeAt(i) & 0xFF)
  7918. }
  7919. return byteArray
  7920. }
  7921.  
  7922. function utf16leToBytes (str, units) {
  7923. var c, hi, lo
  7924. var byteArray = []
  7925. for (var i = 0; i < str.length; ++i) {
  7926. if ((units -= 2) < 0) break
  7927.  
  7928. c = str.charCodeAt(i)
  7929. hi = c >> 8
  7930. lo = c % 256
  7931. byteArray.push(lo)
  7932. byteArray.push(hi)
  7933. }
  7934.  
  7935. return byteArray
  7936. }
  7937.  
  7938. function base64ToBytes (str) {
  7939. return base64.toByteArray(base64clean(str))
  7940. }
  7941.  
  7942. function blitBuffer (src, dst, offset, length) {
  7943. for (var i = 0; i < length; ++i) {
  7944. if ((i + offset >= dst.length) || (i >= src.length)) break
  7945. dst[i + offset] = src[i]
  7946. }
  7947. return i
  7948. }
  7949.  
  7950. function isnan (val) {
  7951. return val !== val // eslint-disable-line no-self-compare
  7952. }
  7953.  
  7954. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  7955. },{"base64-js":12,"ieee754":13,"isarray":14}],"lz4":[function(require,module,exports){
  7956. /**
  7957. * LZ4 based compression and decompression
  7958. * Copyright (c) 2014 Pierre Curto
  7959. * MIT Licensed
  7960. */
  7961.  
  7962. module.exports = require('./static')
  7963.  
  7964. module.exports.version = "0.5.1"
  7965. module.exports.createDecoderStream = require('./decoder_stream')
  7966. module.exports.decode = require('./decoder').LZ4_uncompress
  7967.  
  7968. module.exports.createEncoderStream = require('./encoder_stream')
  7969. module.exports.encode = require('./encoder').LZ4_compress
  7970.  
  7971. // Expose block decoder and encoders
  7972. var bindings = module.exports.utils.bindings
  7973.  
  7974. module.exports.decodeBlock = bindings.uncompress
  7975.  
  7976. module.exports.encodeBound = bindings.compressBound
  7977. module.exports.encodeBlock = bindings.compress
  7978. module.exports.encodeBlockHC = bindings.compressHC
  7979.  
  7980. },{"./decoder":2,"./decoder_stream":3,"./encoder":4,"./encoder_stream":5,"./static":6}]},{},["lz4"]);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址