Ali Ghassemi | fdbdd09 | 2014-06-18 18:32:56 -0700 | [diff] [blame] | 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);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ |
| 2 | /*! |
| 3 | * The buffer module from node.js, for the browser. |
| 4 | * |
| 5 | * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org> |
| 6 | * @license MIT |
| 7 | */ |
| 8 | |
| 9 | var base64 = require('base64-js') |
| 10 | var ieee754 = require('ieee754') |
| 11 | |
| 12 | exports.Buffer = Buffer |
| 13 | exports.SlowBuffer = Buffer |
| 14 | exports.INSPECT_MAX_BYTES = 50 |
| 15 | Buffer.poolSize = 8192 |
| 16 | |
| 17 | /** |
| 18 | * If `Buffer._useTypedArrays`: |
| 19 | * === true Use Uint8Array implementation (fastest) |
| 20 | * === false Use Object implementation (compatible down to IE6) |
| 21 | */ |
| 22 | Buffer._useTypedArrays = (function () { |
| 23 | // Detect if browser supports Typed Arrays. Supported browsers are IE 10+, Firefox 4+, |
| 24 | // Chrome 7+, Safari 5.1+, Opera 11.6+, iOS 4.2+. If the browser does not support adding |
| 25 | // properties to `Uint8Array` instances, then that's the same as no `Uint8Array` support |
| 26 | // because we need to be able to add all the node Buffer API methods. This is an issue |
| 27 | // in Firefox 4-29. Now fixed: https://bugzilla.mozilla.org/show_bug.cgi?id=695438 |
| 28 | try { |
| 29 | var buf = new ArrayBuffer(0) |
| 30 | var arr = new Uint8Array(buf) |
| 31 | arr.foo = function () { return 42 } |
| 32 | return 42 === arr.foo() && |
| 33 | typeof arr.subarray === 'function' // Chrome 9-10 lack `subarray` |
| 34 | } catch (e) { |
| 35 | return false |
| 36 | } |
| 37 | })() |
| 38 | |
| 39 | /** |
| 40 | * Class: Buffer |
| 41 | * ============= |
| 42 | * |
| 43 | * The Buffer constructor returns instances of `Uint8Array` that are augmented |
| 44 | * with function properties for all the node `Buffer` API functions. We use |
| 45 | * `Uint8Array` so that square bracket notation works as expected -- it returns |
| 46 | * a single octet. |
| 47 | * |
| 48 | * By augmenting the instances, we can avoid modifying the `Uint8Array` |
| 49 | * prototype. |
| 50 | */ |
| 51 | function Buffer (subject, encoding, noZero) { |
| 52 | if (!(this instanceof Buffer)) |
| 53 | return new Buffer(subject, encoding, noZero) |
| 54 | |
| 55 | var type = typeof subject |
| 56 | |
| 57 | // Workaround: node's base64 implementation allows for non-padded strings |
| 58 | // while base64-js does not. |
| 59 | if (encoding === 'base64' && type === 'string') { |
| 60 | subject = stringtrim(subject) |
| 61 | while (subject.length % 4 !== 0) { |
| 62 | subject = subject + '=' |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Find the length |
| 67 | var length |
| 68 | if (type === 'number') |
| 69 | length = coerce(subject) |
| 70 | else if (type === 'string') |
| 71 | length = Buffer.byteLength(subject, encoding) |
| 72 | else if (type === 'object') |
| 73 | length = coerce(subject.length) // assume that object is array-like |
| 74 | else |
| 75 | throw new Error('First argument needs to be a number, array or string.') |
| 76 | |
| 77 | var buf |
| 78 | if (Buffer._useTypedArrays) { |
| 79 | // Preferred: Return an augmented `Uint8Array` instance for best performance |
| 80 | buf = Buffer._augment(new Uint8Array(length)) |
| 81 | } else { |
| 82 | // Fallback: Return THIS instance of Buffer (created by `new`) |
| 83 | buf = this |
| 84 | buf.length = length |
| 85 | buf._isBuffer = true |
| 86 | } |
| 87 | |
| 88 | var i |
| 89 | if (Buffer._useTypedArrays && typeof subject.byteLength === 'number') { |
| 90 | // Speed optimization -- use set if we're copying from a typed array |
| 91 | buf._set(subject) |
| 92 | } else if (isArrayish(subject)) { |
| 93 | // Treat array-ish objects as a byte array |
| 94 | if (Buffer.isBuffer(subject)) { |
| 95 | for (i = 0; i < length; i++) |
| 96 | buf[i] = subject.readUInt8(i) |
| 97 | } else { |
| 98 | for (i = 0; i < length; i++) |
| 99 | buf[i] = ((subject[i] % 256) + 256) % 256 |
| 100 | } |
| 101 | } else if (type === 'string') { |
| 102 | buf.write(subject, 0, encoding) |
| 103 | } else if (type === 'number' && !Buffer._useTypedArrays && !noZero) { |
| 104 | for (i = 0; i < length; i++) { |
| 105 | buf[i] = 0 |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return buf |
| 110 | } |
| 111 | |
| 112 | // STATIC METHODS |
| 113 | // ============== |
| 114 | |
| 115 | Buffer.isEncoding = function (encoding) { |
| 116 | switch (String(encoding).toLowerCase()) { |
| 117 | case 'hex': |
| 118 | case 'utf8': |
| 119 | case 'utf-8': |
| 120 | case 'ascii': |
| 121 | case 'binary': |
| 122 | case 'base64': |
| 123 | case 'raw': |
| 124 | case 'ucs2': |
| 125 | case 'ucs-2': |
| 126 | case 'utf16le': |
| 127 | case 'utf-16le': |
| 128 | return true |
| 129 | default: |
| 130 | return false |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | Buffer.isBuffer = function (b) { |
| 135 | return !!(b !== null && b !== undefined && b._isBuffer) |
| 136 | } |
| 137 | |
| 138 | Buffer.byteLength = function (str, encoding) { |
| 139 | var ret |
| 140 | str = str.toString() |
| 141 | switch (encoding || 'utf8') { |
| 142 | case 'hex': |
| 143 | ret = str.length / 2 |
| 144 | break |
| 145 | case 'utf8': |
| 146 | case 'utf-8': |
| 147 | ret = utf8ToBytes(str).length |
| 148 | break |
| 149 | case 'ascii': |
| 150 | case 'binary': |
| 151 | case 'raw': |
| 152 | ret = str.length |
| 153 | break |
| 154 | case 'base64': |
| 155 | ret = base64ToBytes(str).length |
| 156 | break |
| 157 | case 'ucs2': |
| 158 | case 'ucs-2': |
| 159 | case 'utf16le': |
| 160 | case 'utf-16le': |
| 161 | ret = str.length * 2 |
| 162 | break |
| 163 | default: |
| 164 | throw new Error('Unknown encoding') |
| 165 | } |
| 166 | return ret |
| 167 | } |
| 168 | |
| 169 | Buffer.concat = function (list, totalLength) { |
| 170 | assert(isArray(list), 'Usage: Buffer.concat(list[, length])') |
| 171 | |
| 172 | if (list.length === 0) { |
| 173 | return new Buffer(0) |
| 174 | } else if (list.length === 1) { |
| 175 | return list[0] |
| 176 | } |
| 177 | |
| 178 | var i |
| 179 | if (totalLength === undefined) { |
| 180 | totalLength = 0 |
| 181 | for (i = 0; i < list.length; i++) { |
| 182 | totalLength += list[i].length |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | var buf = new Buffer(totalLength) |
| 187 | var pos = 0 |
| 188 | for (i = 0; i < list.length; i++) { |
| 189 | var item = list[i] |
| 190 | item.copy(buf, pos) |
| 191 | pos += item.length |
| 192 | } |
| 193 | return buf |
| 194 | } |
| 195 | |
| 196 | Buffer.compare = function (a, b) { |
| 197 | assert(Buffer.isBuffer(a) && Buffer.isBuffer(b), 'Arguments must be Buffers') |
| 198 | var x = a.length |
| 199 | var y = b.length |
| 200 | for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {} |
| 201 | if (i !== len) { |
| 202 | x = a[i] |
| 203 | y = b[i] |
| 204 | } |
| 205 | if (x < y) { |
| 206 | return -1 |
| 207 | } |
| 208 | if (y < x) { |
| 209 | return 1 |
| 210 | } |
| 211 | return 0 |
| 212 | } |
| 213 | |
| 214 | // BUFFER INSTANCE METHODS |
| 215 | // ======================= |
| 216 | |
| 217 | function hexWrite (buf, string, offset, length) { |
| 218 | offset = Number(offset) || 0 |
| 219 | var remaining = buf.length - offset |
| 220 | if (!length) { |
| 221 | length = remaining |
| 222 | } else { |
| 223 | length = Number(length) |
| 224 | if (length > remaining) { |
| 225 | length = remaining |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // must be an even number of digits |
| 230 | var strLen = string.length |
| 231 | assert(strLen % 2 === 0, 'Invalid hex string') |
| 232 | |
| 233 | if (length > strLen / 2) { |
| 234 | length = strLen / 2 |
| 235 | } |
| 236 | for (var i = 0; i < length; i++) { |
| 237 | var byte = parseInt(string.substr(i * 2, 2), 16) |
| 238 | assert(!isNaN(byte), 'Invalid hex string') |
| 239 | buf[offset + i] = byte |
| 240 | } |
| 241 | return i |
| 242 | } |
| 243 | |
| 244 | function utf8Write (buf, string, offset, length) { |
| 245 | var charsWritten = blitBuffer(utf8ToBytes(string), buf, offset, length) |
| 246 | return charsWritten |
| 247 | } |
| 248 | |
| 249 | function asciiWrite (buf, string, offset, length) { |
| 250 | var charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length) |
| 251 | return charsWritten |
| 252 | } |
| 253 | |
| 254 | function binaryWrite (buf, string, offset, length) { |
| 255 | return asciiWrite(buf, string, offset, length) |
| 256 | } |
| 257 | |
| 258 | function base64Write (buf, string, offset, length) { |
| 259 | var charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length) |
| 260 | return charsWritten |
| 261 | } |
| 262 | |
| 263 | function utf16leWrite (buf, string, offset, length) { |
| 264 | var charsWritten = blitBuffer(utf16leToBytes(string), buf, offset, length) |
| 265 | return charsWritten |
| 266 | } |
| 267 | |
| 268 | Buffer.prototype.write = function (string, offset, length, encoding) { |
| 269 | // Support both (string, offset, length, encoding) |
| 270 | // and the legacy (string, encoding, offset, length) |
| 271 | if (isFinite(offset)) { |
| 272 | if (!isFinite(length)) { |
| 273 | encoding = length |
| 274 | length = undefined |
| 275 | } |
| 276 | } else { // legacy |
| 277 | var swap = encoding |
| 278 | encoding = offset |
| 279 | offset = length |
| 280 | length = swap |
| 281 | } |
| 282 | |
| 283 | offset = Number(offset) || 0 |
| 284 | var remaining = this.length - offset |
| 285 | if (!length) { |
| 286 | length = remaining |
| 287 | } else { |
| 288 | length = Number(length) |
| 289 | if (length > remaining) { |
| 290 | length = remaining |
| 291 | } |
| 292 | } |
| 293 | encoding = String(encoding || 'utf8').toLowerCase() |
| 294 | |
| 295 | var ret |
| 296 | switch (encoding) { |
| 297 | case 'hex': |
| 298 | ret = hexWrite(this, string, offset, length) |
| 299 | break |
| 300 | case 'utf8': |
| 301 | case 'utf-8': |
| 302 | ret = utf8Write(this, string, offset, length) |
| 303 | break |
| 304 | case 'ascii': |
| 305 | ret = asciiWrite(this, string, offset, length) |
| 306 | break |
| 307 | case 'binary': |
| 308 | ret = binaryWrite(this, string, offset, length) |
| 309 | break |
| 310 | case 'base64': |
| 311 | ret = base64Write(this, string, offset, length) |
| 312 | break |
| 313 | case 'ucs2': |
| 314 | case 'ucs-2': |
| 315 | case 'utf16le': |
| 316 | case 'utf-16le': |
| 317 | ret = utf16leWrite(this, string, offset, length) |
| 318 | break |
| 319 | default: |
| 320 | throw new Error('Unknown encoding') |
| 321 | } |
| 322 | return ret |
| 323 | } |
| 324 | |
| 325 | Buffer.prototype.toString = function (encoding, start, end) { |
| 326 | var self = this |
| 327 | |
| 328 | encoding = String(encoding || 'utf8').toLowerCase() |
| 329 | start = Number(start) || 0 |
| 330 | end = (end === undefined) ? self.length : Number(end) |
| 331 | |
| 332 | // Fastpath empty strings |
| 333 | if (end === start) |
| 334 | return '' |
| 335 | |
| 336 | var ret |
| 337 | switch (encoding) { |
| 338 | case 'hex': |
| 339 | ret = hexSlice(self, start, end) |
| 340 | break |
| 341 | case 'utf8': |
| 342 | case 'utf-8': |
| 343 | ret = utf8Slice(self, start, end) |
| 344 | break |
| 345 | case 'ascii': |
| 346 | ret = asciiSlice(self, start, end) |
| 347 | break |
| 348 | case 'binary': |
| 349 | ret = binarySlice(self, start, end) |
| 350 | break |
| 351 | case 'base64': |
| 352 | ret = base64Slice(self, start, end) |
| 353 | break |
| 354 | case 'ucs2': |
| 355 | case 'ucs-2': |
| 356 | case 'utf16le': |
| 357 | case 'utf-16le': |
| 358 | ret = utf16leSlice(self, start, end) |
| 359 | break |
| 360 | default: |
| 361 | throw new Error('Unknown encoding') |
| 362 | } |
| 363 | return ret |
| 364 | } |
| 365 | |
| 366 | Buffer.prototype.toJSON = function () { |
| 367 | return { |
| 368 | type: 'Buffer', |
| 369 | data: Array.prototype.slice.call(this._arr || this, 0) |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | Buffer.prototype.equals = function (b) { |
| 374 | assert(Buffer.isBuffer(b), 'Argument must be a Buffer') |
| 375 | return Buffer.compare(this, b) === 0 |
| 376 | } |
| 377 | |
| 378 | Buffer.prototype.compare = function (b) { |
| 379 | assert(Buffer.isBuffer(b), 'Argument must be a Buffer') |
| 380 | return Buffer.compare(this, b) |
| 381 | } |
| 382 | |
| 383 | // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) |
| 384 | Buffer.prototype.copy = function (target, target_start, start, end) { |
| 385 | var source = this |
| 386 | |
| 387 | if (!start) start = 0 |
| 388 | if (!end && end !== 0) end = this.length |
| 389 | if (!target_start) target_start = 0 |
| 390 | |
| 391 | // Copy 0 bytes; we're done |
| 392 | if (end === start) return |
| 393 | if (target.length === 0 || source.length === 0) return |
| 394 | |
| 395 | // Fatal error conditions |
| 396 | assert(end >= start, 'sourceEnd < sourceStart') |
| 397 | assert(target_start >= 0 && target_start < target.length, |
| 398 | 'targetStart out of bounds') |
| 399 | assert(start >= 0 && start < source.length, 'sourceStart out of bounds') |
| 400 | assert(end >= 0 && end <= source.length, 'sourceEnd out of bounds') |
| 401 | |
| 402 | // Are we oob? |
| 403 | if (end > this.length) |
| 404 | end = this.length |
| 405 | if (target.length - target_start < end - start) |
| 406 | end = target.length - target_start + start |
| 407 | |
| 408 | var len = end - start |
| 409 | |
| 410 | if (len < 100 || !Buffer._useTypedArrays) { |
| 411 | for (var i = 0; i < len; i++) { |
| 412 | target[i + target_start] = this[i + start] |
| 413 | } |
| 414 | } else { |
| 415 | target._set(this.subarray(start, start + len), target_start) |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | function base64Slice (buf, start, end) { |
| 420 | if (start === 0 && end === buf.length) { |
| 421 | return base64.fromByteArray(buf) |
| 422 | } else { |
| 423 | return base64.fromByteArray(buf.slice(start, end)) |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | function utf8Slice (buf, start, end) { |
| 428 | var res = '' |
| 429 | var tmp = '' |
| 430 | end = Math.min(buf.length, end) |
| 431 | |
| 432 | for (var i = start; i < end; i++) { |
| 433 | if (buf[i] <= 0x7F) { |
| 434 | res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i]) |
| 435 | tmp = '' |
| 436 | } else { |
| 437 | tmp += '%' + buf[i].toString(16) |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return res + decodeUtf8Char(tmp) |
| 442 | } |
| 443 | |
| 444 | function asciiSlice (buf, start, end) { |
| 445 | var ret = '' |
| 446 | end = Math.min(buf.length, end) |
| 447 | |
| 448 | for (var i = start; i < end; i++) { |
| 449 | ret += String.fromCharCode(buf[i]) |
| 450 | } |
| 451 | return ret |
| 452 | } |
| 453 | |
| 454 | function binarySlice (buf, start, end) { |
| 455 | return asciiSlice(buf, start, end) |
| 456 | } |
| 457 | |
| 458 | function hexSlice (buf, start, end) { |
| 459 | var len = buf.length |
| 460 | |
| 461 | if (!start || start < 0) start = 0 |
| 462 | if (!end || end < 0 || end > len) end = len |
| 463 | |
| 464 | var out = '' |
| 465 | for (var i = start; i < end; i++) { |
| 466 | out += toHex(buf[i]) |
| 467 | } |
| 468 | return out |
| 469 | } |
| 470 | |
| 471 | function utf16leSlice (buf, start, end) { |
| 472 | var bytes = buf.slice(start, end) |
| 473 | var res = '' |
| 474 | for (var i = 0; i < bytes.length; i += 2) { |
| 475 | res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256) |
| 476 | } |
| 477 | return res |
| 478 | } |
| 479 | |
| 480 | Buffer.prototype.slice = function (start, end) { |
| 481 | var len = this.length |
| 482 | start = clamp(start, len, 0) |
| 483 | end = clamp(end, len, len) |
| 484 | |
| 485 | if (Buffer._useTypedArrays) { |
| 486 | return Buffer._augment(this.subarray(start, end)) |
| 487 | } else { |
| 488 | var sliceLen = end - start |
| 489 | var newBuf = new Buffer(sliceLen, undefined, true) |
| 490 | for (var i = 0; i < sliceLen; i++) { |
| 491 | newBuf[i] = this[i + start] |
| 492 | } |
| 493 | return newBuf |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | // `get` will be removed in Node 0.13+ |
| 498 | Buffer.prototype.get = function (offset) { |
| 499 | console.log('.get() is deprecated. Access using array indexes instead.') |
| 500 | return this.readUInt8(offset) |
| 501 | } |
| 502 | |
| 503 | // `set` will be removed in Node 0.13+ |
| 504 | Buffer.prototype.set = function (v, offset) { |
| 505 | console.log('.set() is deprecated. Access using array indexes instead.') |
| 506 | return this.writeUInt8(v, offset) |
| 507 | } |
| 508 | |
| 509 | Buffer.prototype.readUInt8 = function (offset, noAssert) { |
| 510 | if (!noAssert) { |
| 511 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 512 | assert(offset < this.length, 'Trying to read beyond buffer length') |
| 513 | } |
| 514 | |
| 515 | if (offset >= this.length) |
| 516 | return |
| 517 | |
| 518 | return this[offset] |
| 519 | } |
| 520 | |
| 521 | function readUInt16 (buf, offset, littleEndian, noAssert) { |
| 522 | if (!noAssert) { |
| 523 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 524 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 525 | assert(offset + 1 < buf.length, 'Trying to read beyond buffer length') |
| 526 | } |
| 527 | |
| 528 | var len = buf.length |
| 529 | if (offset >= len) |
| 530 | return |
| 531 | |
| 532 | var val |
| 533 | if (littleEndian) { |
| 534 | val = buf[offset] |
| 535 | if (offset + 1 < len) |
| 536 | val |= buf[offset + 1] << 8 |
| 537 | } else { |
| 538 | val = buf[offset] << 8 |
| 539 | if (offset + 1 < len) |
| 540 | val |= buf[offset + 1] |
| 541 | } |
| 542 | return val |
| 543 | } |
| 544 | |
| 545 | Buffer.prototype.readUInt16LE = function (offset, noAssert) { |
| 546 | return readUInt16(this, offset, true, noAssert) |
| 547 | } |
| 548 | |
| 549 | Buffer.prototype.readUInt16BE = function (offset, noAssert) { |
| 550 | return readUInt16(this, offset, false, noAssert) |
| 551 | } |
| 552 | |
| 553 | function readUInt32 (buf, offset, littleEndian, noAssert) { |
| 554 | if (!noAssert) { |
| 555 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 556 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 557 | assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') |
| 558 | } |
| 559 | |
| 560 | var len = buf.length |
| 561 | if (offset >= len) |
| 562 | return |
| 563 | |
| 564 | var val |
| 565 | if (littleEndian) { |
| 566 | if (offset + 2 < len) |
| 567 | val = buf[offset + 2] << 16 |
| 568 | if (offset + 1 < len) |
| 569 | val |= buf[offset + 1] << 8 |
| 570 | val |= buf[offset] |
| 571 | if (offset + 3 < len) |
| 572 | val = val + (buf[offset + 3] << 24 >>> 0) |
| 573 | } else { |
| 574 | if (offset + 1 < len) |
| 575 | val = buf[offset + 1] << 16 |
| 576 | if (offset + 2 < len) |
| 577 | val |= buf[offset + 2] << 8 |
| 578 | if (offset + 3 < len) |
| 579 | val |= buf[offset + 3] |
| 580 | val = val + (buf[offset] << 24 >>> 0) |
| 581 | } |
| 582 | return val |
| 583 | } |
| 584 | |
| 585 | Buffer.prototype.readUInt32LE = function (offset, noAssert) { |
| 586 | return readUInt32(this, offset, true, noAssert) |
| 587 | } |
| 588 | |
| 589 | Buffer.prototype.readUInt32BE = function (offset, noAssert) { |
| 590 | return readUInt32(this, offset, false, noAssert) |
| 591 | } |
| 592 | |
| 593 | Buffer.prototype.readInt8 = function (offset, noAssert) { |
| 594 | if (!noAssert) { |
| 595 | assert(offset !== undefined && offset !== null, |
| 596 | 'missing offset') |
| 597 | assert(offset < this.length, 'Trying to read beyond buffer length') |
| 598 | } |
| 599 | |
| 600 | if (offset >= this.length) |
| 601 | return |
| 602 | |
| 603 | var neg = this[offset] & 0x80 |
| 604 | if (neg) |
| 605 | return (0xff - this[offset] + 1) * -1 |
| 606 | else |
| 607 | return this[offset] |
| 608 | } |
| 609 | |
| 610 | function readInt16 (buf, offset, littleEndian, noAssert) { |
| 611 | if (!noAssert) { |
| 612 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 613 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 614 | assert(offset + 1 < buf.length, 'Trying to read beyond buffer length') |
| 615 | } |
| 616 | |
| 617 | var len = buf.length |
| 618 | if (offset >= len) |
| 619 | return |
| 620 | |
| 621 | var val = readUInt16(buf, offset, littleEndian, true) |
| 622 | var neg = val & 0x8000 |
| 623 | if (neg) |
| 624 | return (0xffff - val + 1) * -1 |
| 625 | else |
| 626 | return val |
| 627 | } |
| 628 | |
| 629 | Buffer.prototype.readInt16LE = function (offset, noAssert) { |
| 630 | return readInt16(this, offset, true, noAssert) |
| 631 | } |
| 632 | |
| 633 | Buffer.prototype.readInt16BE = function (offset, noAssert) { |
| 634 | return readInt16(this, offset, false, noAssert) |
| 635 | } |
| 636 | |
| 637 | function readInt32 (buf, offset, littleEndian, noAssert) { |
| 638 | if (!noAssert) { |
| 639 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 640 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 641 | assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') |
| 642 | } |
| 643 | |
| 644 | var len = buf.length |
| 645 | if (offset >= len) |
| 646 | return |
| 647 | |
| 648 | var val = readUInt32(buf, offset, littleEndian, true) |
| 649 | var neg = val & 0x80000000 |
| 650 | if (neg) |
| 651 | return (0xffffffff - val + 1) * -1 |
| 652 | else |
| 653 | return val |
| 654 | } |
| 655 | |
| 656 | Buffer.prototype.readInt32LE = function (offset, noAssert) { |
| 657 | return readInt32(this, offset, true, noAssert) |
| 658 | } |
| 659 | |
| 660 | Buffer.prototype.readInt32BE = function (offset, noAssert) { |
| 661 | return readInt32(this, offset, false, noAssert) |
| 662 | } |
| 663 | |
| 664 | function readFloat (buf, offset, littleEndian, noAssert) { |
| 665 | if (!noAssert) { |
| 666 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 667 | assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') |
| 668 | } |
| 669 | |
| 670 | return ieee754.read(buf, offset, littleEndian, 23, 4) |
| 671 | } |
| 672 | |
| 673 | Buffer.prototype.readFloatLE = function (offset, noAssert) { |
| 674 | return readFloat(this, offset, true, noAssert) |
| 675 | } |
| 676 | |
| 677 | Buffer.prototype.readFloatBE = function (offset, noAssert) { |
| 678 | return readFloat(this, offset, false, noAssert) |
| 679 | } |
| 680 | |
| 681 | function readDouble (buf, offset, littleEndian, noAssert) { |
| 682 | if (!noAssert) { |
| 683 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 684 | assert(offset + 7 < buf.length, 'Trying to read beyond buffer length') |
| 685 | } |
| 686 | |
| 687 | return ieee754.read(buf, offset, littleEndian, 52, 8) |
| 688 | } |
| 689 | |
| 690 | Buffer.prototype.readDoubleLE = function (offset, noAssert) { |
| 691 | return readDouble(this, offset, true, noAssert) |
| 692 | } |
| 693 | |
| 694 | Buffer.prototype.readDoubleBE = function (offset, noAssert) { |
| 695 | return readDouble(this, offset, false, noAssert) |
| 696 | } |
| 697 | |
| 698 | Buffer.prototype.writeUInt8 = function (value, offset, noAssert) { |
| 699 | if (!noAssert) { |
| 700 | assert(value !== undefined && value !== null, 'missing value') |
| 701 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 702 | assert(offset < this.length, 'trying to write beyond buffer length') |
| 703 | verifuint(value, 0xff) |
| 704 | } |
| 705 | |
| 706 | if (offset >= this.length) return |
| 707 | |
| 708 | this[offset] = value |
| 709 | return offset + 1 |
| 710 | } |
| 711 | |
| 712 | function writeUInt16 (buf, value, offset, littleEndian, noAssert) { |
| 713 | if (!noAssert) { |
| 714 | assert(value !== undefined && value !== null, 'missing value') |
| 715 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 716 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 717 | assert(offset + 1 < buf.length, 'trying to write beyond buffer length') |
| 718 | verifuint(value, 0xffff) |
| 719 | } |
| 720 | |
| 721 | var len = buf.length |
| 722 | if (offset >= len) |
| 723 | return |
| 724 | |
| 725 | for (var i = 0, j = Math.min(len - offset, 2); i < j; i++) { |
| 726 | buf[offset + i] = |
| 727 | (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>> |
| 728 | (littleEndian ? i : 1 - i) * 8 |
| 729 | } |
| 730 | return offset + 2 |
| 731 | } |
| 732 | |
| 733 | Buffer.prototype.writeUInt16LE = function (value, offset, noAssert) { |
| 734 | return writeUInt16(this, value, offset, true, noAssert) |
| 735 | } |
| 736 | |
| 737 | Buffer.prototype.writeUInt16BE = function (value, offset, noAssert) { |
| 738 | return writeUInt16(this, value, offset, false, noAssert) |
| 739 | } |
| 740 | |
| 741 | function writeUInt32 (buf, value, offset, littleEndian, noAssert) { |
| 742 | if (!noAssert) { |
| 743 | assert(value !== undefined && value !== null, 'missing value') |
| 744 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 745 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 746 | assert(offset + 3 < buf.length, 'trying to write beyond buffer length') |
| 747 | verifuint(value, 0xffffffff) |
| 748 | } |
| 749 | |
| 750 | var len = buf.length |
| 751 | if (offset >= len) |
| 752 | return |
| 753 | |
| 754 | for (var i = 0, j = Math.min(len - offset, 4); i < j; i++) { |
| 755 | buf[offset + i] = |
| 756 | (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff |
| 757 | } |
| 758 | return offset + 4 |
| 759 | } |
| 760 | |
| 761 | Buffer.prototype.writeUInt32LE = function (value, offset, noAssert) { |
| 762 | return writeUInt32(this, value, offset, true, noAssert) |
| 763 | } |
| 764 | |
| 765 | Buffer.prototype.writeUInt32BE = function (value, offset, noAssert) { |
| 766 | return writeUInt32(this, value, offset, false, noAssert) |
| 767 | } |
| 768 | |
| 769 | Buffer.prototype.writeInt8 = function (value, offset, noAssert) { |
| 770 | if (!noAssert) { |
| 771 | assert(value !== undefined && value !== null, 'missing value') |
| 772 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 773 | assert(offset < this.length, 'Trying to write beyond buffer length') |
| 774 | verifsint(value, 0x7f, -0x80) |
| 775 | } |
| 776 | |
| 777 | if (offset >= this.length) |
| 778 | return |
| 779 | |
| 780 | if (value >= 0) |
| 781 | this.writeUInt8(value, offset, noAssert) |
| 782 | else |
| 783 | this.writeUInt8(0xff + value + 1, offset, noAssert) |
| 784 | return offset + 1 |
| 785 | } |
| 786 | |
| 787 | function writeInt16 (buf, value, offset, littleEndian, noAssert) { |
| 788 | if (!noAssert) { |
| 789 | assert(value !== undefined && value !== null, 'missing value') |
| 790 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 791 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 792 | assert(offset + 1 < buf.length, 'Trying to write beyond buffer length') |
| 793 | verifsint(value, 0x7fff, -0x8000) |
| 794 | } |
| 795 | |
| 796 | var len = buf.length |
| 797 | if (offset >= len) |
| 798 | return |
| 799 | |
| 800 | if (value >= 0) |
| 801 | writeUInt16(buf, value, offset, littleEndian, noAssert) |
| 802 | else |
| 803 | writeUInt16(buf, 0xffff + value + 1, offset, littleEndian, noAssert) |
| 804 | return offset + 2 |
| 805 | } |
| 806 | |
| 807 | Buffer.prototype.writeInt16LE = function (value, offset, noAssert) { |
| 808 | return writeInt16(this, value, offset, true, noAssert) |
| 809 | } |
| 810 | |
| 811 | Buffer.prototype.writeInt16BE = function (value, offset, noAssert) { |
| 812 | return writeInt16(this, value, offset, false, noAssert) |
| 813 | } |
| 814 | |
| 815 | function writeInt32 (buf, value, offset, littleEndian, noAssert) { |
| 816 | if (!noAssert) { |
| 817 | assert(value !== undefined && value !== null, 'missing value') |
| 818 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 819 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 820 | assert(offset + 3 < buf.length, 'Trying to write beyond buffer length') |
| 821 | verifsint(value, 0x7fffffff, -0x80000000) |
| 822 | } |
| 823 | |
| 824 | var len = buf.length |
| 825 | if (offset >= len) |
| 826 | return |
| 827 | |
| 828 | if (value >= 0) |
| 829 | writeUInt32(buf, value, offset, littleEndian, noAssert) |
| 830 | else |
| 831 | writeUInt32(buf, 0xffffffff + value + 1, offset, littleEndian, noAssert) |
| 832 | return offset + 4 |
| 833 | } |
| 834 | |
| 835 | Buffer.prototype.writeInt32LE = function (value, offset, noAssert) { |
| 836 | return writeInt32(this, value, offset, true, noAssert) |
| 837 | } |
| 838 | |
| 839 | Buffer.prototype.writeInt32BE = function (value, offset, noAssert) { |
| 840 | return writeInt32(this, value, offset, false, noAssert) |
| 841 | } |
| 842 | |
| 843 | function writeFloat (buf, value, offset, littleEndian, noAssert) { |
| 844 | if (!noAssert) { |
| 845 | assert(value !== undefined && value !== null, 'missing value') |
| 846 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 847 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 848 | assert(offset + 3 < buf.length, 'Trying to write beyond buffer length') |
| 849 | verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38) |
| 850 | } |
| 851 | |
| 852 | var len = buf.length |
| 853 | if (offset >= len) |
| 854 | return |
| 855 | |
| 856 | ieee754.write(buf, value, offset, littleEndian, 23, 4) |
| 857 | return offset + 4 |
| 858 | } |
| 859 | |
| 860 | Buffer.prototype.writeFloatLE = function (value, offset, noAssert) { |
| 861 | return writeFloat(this, value, offset, true, noAssert) |
| 862 | } |
| 863 | |
| 864 | Buffer.prototype.writeFloatBE = function (value, offset, noAssert) { |
| 865 | return writeFloat(this, value, offset, false, noAssert) |
| 866 | } |
| 867 | |
| 868 | function writeDouble (buf, value, offset, littleEndian, noAssert) { |
| 869 | if (!noAssert) { |
| 870 | assert(value !== undefined && value !== null, 'missing value') |
| 871 | assert(typeof littleEndian === 'boolean', 'missing or invalid endian') |
| 872 | assert(offset !== undefined && offset !== null, 'missing offset') |
| 873 | assert(offset + 7 < buf.length, |
| 874 | 'Trying to write beyond buffer length') |
| 875 | verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308) |
| 876 | } |
| 877 | |
| 878 | var len = buf.length |
| 879 | if (offset >= len) |
| 880 | return |
| 881 | |
| 882 | ieee754.write(buf, value, offset, littleEndian, 52, 8) |
| 883 | return offset + 8 |
| 884 | } |
| 885 | |
| 886 | Buffer.prototype.writeDoubleLE = function (value, offset, noAssert) { |
| 887 | return writeDouble(this, value, offset, true, noAssert) |
| 888 | } |
| 889 | |
| 890 | Buffer.prototype.writeDoubleBE = function (value, offset, noAssert) { |
| 891 | return writeDouble(this, value, offset, false, noAssert) |
| 892 | } |
| 893 | |
| 894 | // fill(value, start=0, end=buffer.length) |
| 895 | Buffer.prototype.fill = function (value, start, end) { |
| 896 | if (!value) value = 0 |
| 897 | if (!start) start = 0 |
| 898 | if (!end) end = this.length |
| 899 | |
| 900 | assert(end >= start, 'end < start') |
| 901 | |
| 902 | // Fill 0 bytes; we're done |
| 903 | if (end === start) return |
| 904 | if (this.length === 0) return |
| 905 | |
| 906 | assert(start >= 0 && start < this.length, 'start out of bounds') |
| 907 | assert(end >= 0 && end <= this.length, 'end out of bounds') |
| 908 | |
| 909 | var i |
| 910 | if (typeof value === 'number') { |
| 911 | for (i = start; i < end; i++) { |
| 912 | this[i] = value |
| 913 | } |
| 914 | } else { |
| 915 | var bytes = utf8ToBytes(value.toString()) |
| 916 | var len = bytes.length |
| 917 | for (i = start; i < end; i++) { |
| 918 | this[i] = bytes[i % len] |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | return this |
| 923 | } |
| 924 | |
| 925 | Buffer.prototype.inspect = function () { |
| 926 | var out = [] |
| 927 | var len = this.length |
| 928 | for (var i = 0; i < len; i++) { |
| 929 | out[i] = toHex(this[i]) |
| 930 | if (i === exports.INSPECT_MAX_BYTES) { |
| 931 | out[i + 1] = '...' |
| 932 | break |
| 933 | } |
| 934 | } |
| 935 | return '<Buffer ' + out.join(' ') + '>' |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance. |
| 940 | * Added in Node 0.12. Only available in browsers that support ArrayBuffer. |
| 941 | */ |
| 942 | Buffer.prototype.toArrayBuffer = function () { |
| 943 | if (typeof Uint8Array !== 'undefined') { |
| 944 | if (Buffer._useTypedArrays) { |
| 945 | return (new Buffer(this)).buffer |
| 946 | } else { |
| 947 | var buf = new Uint8Array(this.length) |
| 948 | for (var i = 0, len = buf.length; i < len; i += 1) { |
| 949 | buf[i] = this[i] |
| 950 | } |
| 951 | return buf.buffer |
| 952 | } |
| 953 | } else { |
| 954 | throw new Error('Buffer.toArrayBuffer not supported in this browser') |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | // HELPER FUNCTIONS |
| 959 | // ================ |
| 960 | |
| 961 | var BP = Buffer.prototype |
| 962 | |
| 963 | /** |
| 964 | * Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods |
| 965 | */ |
| 966 | Buffer._augment = function (arr) { |
| 967 | arr._isBuffer = true |
| 968 | |
| 969 | // save reference to original Uint8Array get/set methods before overwriting |
| 970 | arr._get = arr.get |
| 971 | arr._set = arr.set |
| 972 | |
| 973 | // deprecated, will be removed in node 0.13+ |
| 974 | arr.get = BP.get |
| 975 | arr.set = BP.set |
| 976 | |
| 977 | arr.write = BP.write |
| 978 | arr.toString = BP.toString |
| 979 | arr.toLocaleString = BP.toString |
| 980 | arr.toJSON = BP.toJSON |
| 981 | arr.equals = BP.equals |
| 982 | arr.compare = BP.compare |
| 983 | arr.copy = BP.copy |
| 984 | arr.slice = BP.slice |
| 985 | arr.readUInt8 = BP.readUInt8 |
| 986 | arr.readUInt16LE = BP.readUInt16LE |
| 987 | arr.readUInt16BE = BP.readUInt16BE |
| 988 | arr.readUInt32LE = BP.readUInt32LE |
| 989 | arr.readUInt32BE = BP.readUInt32BE |
| 990 | arr.readInt8 = BP.readInt8 |
| 991 | arr.readInt16LE = BP.readInt16LE |
| 992 | arr.readInt16BE = BP.readInt16BE |
| 993 | arr.readInt32LE = BP.readInt32LE |
| 994 | arr.readInt32BE = BP.readInt32BE |
| 995 | arr.readFloatLE = BP.readFloatLE |
| 996 | arr.readFloatBE = BP.readFloatBE |
| 997 | arr.readDoubleLE = BP.readDoubleLE |
| 998 | arr.readDoubleBE = BP.readDoubleBE |
| 999 | arr.writeUInt8 = BP.writeUInt8 |
| 1000 | arr.writeUInt16LE = BP.writeUInt16LE |
| 1001 | arr.writeUInt16BE = BP.writeUInt16BE |
| 1002 | arr.writeUInt32LE = BP.writeUInt32LE |
| 1003 | arr.writeUInt32BE = BP.writeUInt32BE |
| 1004 | arr.writeInt8 = BP.writeInt8 |
| 1005 | arr.writeInt16LE = BP.writeInt16LE |
| 1006 | arr.writeInt16BE = BP.writeInt16BE |
| 1007 | arr.writeInt32LE = BP.writeInt32LE |
| 1008 | arr.writeInt32BE = BP.writeInt32BE |
| 1009 | arr.writeFloatLE = BP.writeFloatLE |
| 1010 | arr.writeFloatBE = BP.writeFloatBE |
| 1011 | arr.writeDoubleLE = BP.writeDoubleLE |
| 1012 | arr.writeDoubleBE = BP.writeDoubleBE |
| 1013 | arr.fill = BP.fill |
| 1014 | arr.inspect = BP.inspect |
| 1015 | arr.toArrayBuffer = BP.toArrayBuffer |
| 1016 | |
| 1017 | return arr |
| 1018 | } |
| 1019 | |
| 1020 | function stringtrim (str) { |
| 1021 | if (str.trim) return str.trim() |
| 1022 | return str.replace(/^\s+|\s+$/g, '') |
| 1023 | } |
| 1024 | |
| 1025 | // slice(start, end) |
| 1026 | function clamp (index, len, defaultValue) { |
| 1027 | if (typeof index !== 'number') return defaultValue |
| 1028 | index = ~~index; // Coerce to integer. |
| 1029 | if (index >= len) return len |
| 1030 | if (index >= 0) return index |
| 1031 | index += len |
| 1032 | if (index >= 0) return index |
| 1033 | return 0 |
| 1034 | } |
| 1035 | |
| 1036 | function coerce (length) { |
| 1037 | // Coerce length to a number (possibly NaN), round up |
| 1038 | // in case it's fractional (e.g. 123.456) then do a |
| 1039 | // double negate to coerce a NaN to 0. Easy, right? |
| 1040 | length = ~~Math.ceil(+length) |
| 1041 | return length < 0 ? 0 : length |
| 1042 | } |
| 1043 | |
| 1044 | function isArray (subject) { |
| 1045 | return (Array.isArray || function (subject) { |
| 1046 | return Object.prototype.toString.call(subject) === '[object Array]' |
| 1047 | })(subject) |
| 1048 | } |
| 1049 | |
| 1050 | function isArrayish (subject) { |
| 1051 | return isArray(subject) || Buffer.isBuffer(subject) || |
| 1052 | subject && typeof subject === 'object' && |
| 1053 | typeof subject.length === 'number' |
| 1054 | } |
| 1055 | |
| 1056 | function toHex (n) { |
| 1057 | if (n < 16) return '0' + n.toString(16) |
| 1058 | return n.toString(16) |
| 1059 | } |
| 1060 | |
| 1061 | function utf8ToBytes (str) { |
| 1062 | var byteArray = [] |
| 1063 | for (var i = 0; i < str.length; i++) { |
| 1064 | var b = str.charCodeAt(i) |
| 1065 | if (b <= 0x7F) { |
| 1066 | byteArray.push(b) |
| 1067 | } else { |
| 1068 | var start = i |
| 1069 | if (b >= 0xD800 && b <= 0xDFFF) i++ |
| 1070 | var h = encodeURIComponent(str.slice(start, i+1)).substr(1).split('%') |
| 1071 | for (var j = 0; j < h.length; j++) { |
| 1072 | byteArray.push(parseInt(h[j], 16)) |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | return byteArray |
| 1077 | } |
| 1078 | |
| 1079 | function asciiToBytes (str) { |
| 1080 | var byteArray = [] |
| 1081 | for (var i = 0; i < str.length; i++) { |
| 1082 | // Node's code seems to be doing this and not & 0x7F.. |
| 1083 | byteArray.push(str.charCodeAt(i) & 0xFF) |
| 1084 | } |
| 1085 | return byteArray |
| 1086 | } |
| 1087 | |
| 1088 | function utf16leToBytes (str) { |
| 1089 | var c, hi, lo |
| 1090 | var byteArray = [] |
| 1091 | for (var i = 0; i < str.length; i++) { |
| 1092 | c = str.charCodeAt(i) |
| 1093 | hi = c >> 8 |
| 1094 | lo = c % 256 |
| 1095 | byteArray.push(lo) |
| 1096 | byteArray.push(hi) |
| 1097 | } |
| 1098 | |
| 1099 | return byteArray |
| 1100 | } |
| 1101 | |
| 1102 | function base64ToBytes (str) { |
| 1103 | return base64.toByteArray(str) |
| 1104 | } |
| 1105 | |
| 1106 | function blitBuffer (src, dst, offset, length) { |
| 1107 | for (var i = 0; i < length; i++) { |
| 1108 | if ((i + offset >= dst.length) || (i >= src.length)) |
| 1109 | break |
| 1110 | dst[i + offset] = src[i] |
| 1111 | } |
| 1112 | return i |
| 1113 | } |
| 1114 | |
| 1115 | function decodeUtf8Char (str) { |
| 1116 | try { |
| 1117 | return decodeURIComponent(str) |
| 1118 | } catch (err) { |
| 1119 | return String.fromCharCode(0xFFFD) // UTF 8 invalid char |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * We have to make sure that the value is a valid integer. This means that it |
| 1125 | * is non-negative. It has no fractional component and that it does not |
| 1126 | * exceed the maximum allowed value. |
| 1127 | */ |
| 1128 | function verifuint (value, max) { |
| 1129 | assert(typeof value === 'number', 'cannot write a non-number as a number') |
| 1130 | assert(value >= 0, 'specified a negative value for writing an unsigned value') |
| 1131 | assert(value <= max, 'value is larger than maximum value for type') |
| 1132 | assert(Math.floor(value) === value, 'value has a fractional component') |
| 1133 | } |
| 1134 | |
| 1135 | function verifsint (value, max, min) { |
| 1136 | assert(typeof value === 'number', 'cannot write a non-number as a number') |
| 1137 | assert(value <= max, 'value larger than maximum allowed value') |
| 1138 | assert(value >= min, 'value smaller than minimum allowed value') |
| 1139 | assert(Math.floor(value) === value, 'value has a fractional component') |
| 1140 | } |
| 1141 | |
| 1142 | function verifIEEE754 (value, max, min) { |
| 1143 | assert(typeof value === 'number', 'cannot write a non-number as a number') |
| 1144 | assert(value <= max, 'value larger than maximum allowed value') |
| 1145 | assert(value >= min, 'value smaller than minimum allowed value') |
| 1146 | } |
| 1147 | |
| 1148 | function assert (test, message) { |
| 1149 | if (!test) throw new Error(message || 'Failed assertion') |
| 1150 | } |
| 1151 | |
| 1152 | },{"base64-js":2,"ieee754":3}],2:[function(require,module,exports){ |
| 1153 | var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
| 1154 | |
| 1155 | ;(function (exports) { |
| 1156 | 'use strict'; |
| 1157 | |
| 1158 | var Arr = (typeof Uint8Array !== 'undefined') |
| 1159 | ? Uint8Array |
| 1160 | : Array |
| 1161 | |
| 1162 | var PLUS = '+'.charCodeAt(0) |
| 1163 | var SLASH = '/'.charCodeAt(0) |
| 1164 | var NUMBER = '0'.charCodeAt(0) |
| 1165 | var LOWER = 'a'.charCodeAt(0) |
| 1166 | var UPPER = 'A'.charCodeAt(0) |
| 1167 | |
| 1168 | function decode (elt) { |
| 1169 | var code = elt.charCodeAt(0) |
| 1170 | if (code === PLUS) |
| 1171 | return 62 // '+' |
| 1172 | if (code === SLASH) |
| 1173 | return 63 // '/' |
| 1174 | if (code < NUMBER) |
| 1175 | return -1 //no match |
| 1176 | if (code < NUMBER + 10) |
| 1177 | return code - NUMBER + 26 + 26 |
| 1178 | if (code < UPPER + 26) |
| 1179 | return code - UPPER |
| 1180 | if (code < LOWER + 26) |
| 1181 | return code - LOWER + 26 |
| 1182 | } |
| 1183 | |
| 1184 | function b64ToByteArray (b64) { |
| 1185 | var i, j, l, tmp, placeHolders, arr |
| 1186 | |
| 1187 | if (b64.length % 4 > 0) { |
| 1188 | throw new Error('Invalid string. Length must be a multiple of 4') |
| 1189 | } |
| 1190 | |
| 1191 | // the number of equal signs (place holders) |
| 1192 | // if there are two placeholders, than the two characters before it |
| 1193 | // represent one byte |
| 1194 | // if there is only one, then the three characters before it represent 2 bytes |
| 1195 | // this is just a cheap hack to not do indexOf twice |
| 1196 | var len = b64.length |
| 1197 | placeHolders = '=' === b64.charAt(len - 2) ? 2 : '=' === b64.charAt(len - 1) ? 1 : 0 |
| 1198 | |
| 1199 | // base64 is 4/3 + up to two characters of the original data |
| 1200 | arr = new Arr(b64.length * 3 / 4 - placeHolders) |
| 1201 | |
| 1202 | // if there are placeholders, only get up to the last complete 4 chars |
| 1203 | l = placeHolders > 0 ? b64.length - 4 : b64.length |
| 1204 | |
| 1205 | var L = 0 |
| 1206 | |
| 1207 | function push (v) { |
| 1208 | arr[L++] = v |
| 1209 | } |
| 1210 | |
| 1211 | for (i = 0, j = 0; i < l; i += 4, j += 3) { |
| 1212 | tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3)) |
| 1213 | push((tmp & 0xFF0000) >> 16) |
| 1214 | push((tmp & 0xFF00) >> 8) |
| 1215 | push(tmp & 0xFF) |
| 1216 | } |
| 1217 | |
| 1218 | if (placeHolders === 2) { |
| 1219 | tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4) |
| 1220 | push(tmp & 0xFF) |
| 1221 | } else if (placeHolders === 1) { |
| 1222 | tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2) |
| 1223 | push((tmp >> 8) & 0xFF) |
| 1224 | push(tmp & 0xFF) |
| 1225 | } |
| 1226 | |
| 1227 | return arr |
| 1228 | } |
| 1229 | |
| 1230 | function uint8ToBase64 (uint8) { |
| 1231 | var i, |
| 1232 | extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes |
| 1233 | output = "", |
| 1234 | temp, length |
| 1235 | |
| 1236 | function encode (num) { |
| 1237 | return lookup.charAt(num) |
| 1238 | } |
| 1239 | |
| 1240 | function tripletToBase64 (num) { |
| 1241 | return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F) |
| 1242 | } |
| 1243 | |
| 1244 | // go through the array every three bytes, we'll deal with trailing stuff later |
| 1245 | for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) { |
| 1246 | temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) |
| 1247 | output += tripletToBase64(temp) |
| 1248 | } |
| 1249 | |
| 1250 | // pad the end with zeros, but make sure to not forget the extra bytes |
| 1251 | switch (extraBytes) { |
| 1252 | case 1: |
| 1253 | temp = uint8[uint8.length - 1] |
| 1254 | output += encode(temp >> 2) |
| 1255 | output += encode((temp << 4) & 0x3F) |
| 1256 | output += '==' |
| 1257 | break |
| 1258 | case 2: |
| 1259 | temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]) |
| 1260 | output += encode(temp >> 10) |
| 1261 | output += encode((temp >> 4) & 0x3F) |
| 1262 | output += encode((temp << 2) & 0x3F) |
| 1263 | output += '=' |
| 1264 | break |
| 1265 | } |
| 1266 | |
| 1267 | return output |
| 1268 | } |
| 1269 | |
| 1270 | exports.toByteArray = b64ToByteArray |
| 1271 | exports.fromByteArray = uint8ToBase64 |
| 1272 | }(typeof exports === 'undefined' ? (this.base64js = {}) : exports)) |
| 1273 | |
| 1274 | },{}],3:[function(require,module,exports){ |
| 1275 | exports.read = function(buffer, offset, isLE, mLen, nBytes) { |
| 1276 | var e, m, |
| 1277 | eLen = nBytes * 8 - mLen - 1, |
| 1278 | eMax = (1 << eLen) - 1, |
| 1279 | eBias = eMax >> 1, |
| 1280 | nBits = -7, |
| 1281 | i = isLE ? (nBytes - 1) : 0, |
| 1282 | d = isLE ? -1 : 1, |
| 1283 | s = buffer[offset + i]; |
| 1284 | |
| 1285 | i += d; |
| 1286 | |
| 1287 | e = s & ((1 << (-nBits)) - 1); |
| 1288 | s >>= (-nBits); |
| 1289 | nBits += eLen; |
| 1290 | for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8); |
| 1291 | |
| 1292 | m = e & ((1 << (-nBits)) - 1); |
| 1293 | e >>= (-nBits); |
| 1294 | nBits += mLen; |
| 1295 | for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8); |
| 1296 | |
| 1297 | if (e === 0) { |
| 1298 | e = 1 - eBias; |
| 1299 | } else if (e === eMax) { |
| 1300 | return m ? NaN : ((s ? -1 : 1) * Infinity); |
| 1301 | } else { |
| 1302 | m = m + Math.pow(2, mLen); |
| 1303 | e = e - eBias; |
| 1304 | } |
| 1305 | return (s ? -1 : 1) * m * Math.pow(2, e - mLen); |
| 1306 | }; |
| 1307 | |
| 1308 | exports.write = function(buffer, value, offset, isLE, mLen, nBytes) { |
| 1309 | var e, m, c, |
| 1310 | eLen = nBytes * 8 - mLen - 1, |
| 1311 | eMax = (1 << eLen) - 1, |
| 1312 | eBias = eMax >> 1, |
| 1313 | rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0), |
| 1314 | i = isLE ? 0 : (nBytes - 1), |
| 1315 | d = isLE ? 1 : -1, |
| 1316 | s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0; |
| 1317 | |
| 1318 | value = Math.abs(value); |
| 1319 | |
| 1320 | if (isNaN(value) || value === Infinity) { |
| 1321 | m = isNaN(value) ? 1 : 0; |
| 1322 | e = eMax; |
| 1323 | } else { |
| 1324 | e = Math.floor(Math.log(value) / Math.LN2); |
| 1325 | if (value * (c = Math.pow(2, -e)) < 1) { |
| 1326 | e--; |
| 1327 | c *= 2; |
| 1328 | } |
| 1329 | if (e + eBias >= 1) { |
| 1330 | value += rt / c; |
| 1331 | } else { |
| 1332 | value += rt * Math.pow(2, 1 - eBias); |
| 1333 | } |
| 1334 | if (value * c >= 2) { |
| 1335 | e++; |
| 1336 | c /= 2; |
| 1337 | } |
| 1338 | |
| 1339 | if (e + eBias >= eMax) { |
| 1340 | m = 0; |
| 1341 | e = eMax; |
| 1342 | } else if (e + eBias >= 1) { |
| 1343 | m = (value * c - 1) * Math.pow(2, mLen); |
| 1344 | e = e + eBias; |
| 1345 | } else { |
| 1346 | m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen); |
| 1347 | e = 0; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8); |
| 1352 | |
| 1353 | e = (e << mLen) | m; |
| 1354 | eLen += mLen; |
| 1355 | for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8); |
| 1356 | |
| 1357 | buffer[offset + i - d] |= s * 128; |
| 1358 | }; |
| 1359 | |
| 1360 | },{}],4:[function(require,module,exports){ |
| 1361 | // Copyright Joyent, Inc. and other Node contributors. |
| 1362 | // |
| 1363 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 1364 | // copy of this software and associated documentation files (the |
| 1365 | // "Software"), to deal in the Software without restriction, including |
| 1366 | // without limitation the rights to use, copy, modify, merge, publish, |
| 1367 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 1368 | // persons to whom the Software is furnished to do so, subject to the |
| 1369 | // following conditions: |
| 1370 | // |
| 1371 | // The above copyright notice and this permission notice shall be included |
| 1372 | // in all copies or substantial portions of the Software. |
| 1373 | // |
| 1374 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 1375 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 1376 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 1377 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 1378 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 1379 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 1380 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 1381 | |
| 1382 | function EventEmitter() { |
| 1383 | this._events = this._events || {}; |
| 1384 | this._maxListeners = this._maxListeners || undefined; |
| 1385 | } |
| 1386 | module.exports = EventEmitter; |
| 1387 | |
| 1388 | // Backwards-compat with node 0.10.x |
| 1389 | EventEmitter.EventEmitter = EventEmitter; |
| 1390 | |
| 1391 | EventEmitter.prototype._events = undefined; |
| 1392 | EventEmitter.prototype._maxListeners = undefined; |
| 1393 | |
| 1394 | // By default EventEmitters will print a warning if more than 10 listeners are |
| 1395 | // added to it. This is a useful default which helps finding memory leaks. |
| 1396 | EventEmitter.defaultMaxListeners = 10; |
| 1397 | |
| 1398 | // Obviously not all Emitters should be limited to 10. This function allows |
| 1399 | // that to be increased. Set to zero for unlimited. |
| 1400 | EventEmitter.prototype.setMaxListeners = function(n) { |
| 1401 | if (!isNumber(n) || n < 0 || isNaN(n)) |
| 1402 | throw TypeError('n must be a positive number'); |
| 1403 | this._maxListeners = n; |
| 1404 | return this; |
| 1405 | }; |
| 1406 | |
| 1407 | EventEmitter.prototype.emit = function(type) { |
| 1408 | var er, handler, len, args, i, listeners; |
| 1409 | |
| 1410 | if (!this._events) |
| 1411 | this._events = {}; |
| 1412 | |
| 1413 | // If there is no 'error' event listener then throw. |
| 1414 | if (type === 'error') { |
| 1415 | if (!this._events.error || |
| 1416 | (isObject(this._events.error) && !this._events.error.length)) { |
| 1417 | er = arguments[1]; |
| 1418 | if (er instanceof Error) { |
| 1419 | throw er; // Unhandled 'error' event |
| 1420 | } else { |
| 1421 | throw TypeError('Uncaught, unspecified "error" event.'); |
| 1422 | } |
| 1423 | return false; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | handler = this._events[type]; |
| 1428 | |
| 1429 | if (isUndefined(handler)) |
| 1430 | return false; |
| 1431 | |
| 1432 | if (isFunction(handler)) { |
| 1433 | switch (arguments.length) { |
| 1434 | // fast cases |
| 1435 | case 1: |
| 1436 | handler.call(this); |
| 1437 | break; |
| 1438 | case 2: |
| 1439 | handler.call(this, arguments[1]); |
| 1440 | break; |
| 1441 | case 3: |
| 1442 | handler.call(this, arguments[1], arguments[2]); |
| 1443 | break; |
| 1444 | // slower |
| 1445 | default: |
| 1446 | len = arguments.length; |
| 1447 | args = new Array(len - 1); |
| 1448 | for (i = 1; i < len; i++) |
| 1449 | args[i - 1] = arguments[i]; |
| 1450 | handler.apply(this, args); |
| 1451 | } |
| 1452 | } else if (isObject(handler)) { |
| 1453 | len = arguments.length; |
| 1454 | args = new Array(len - 1); |
| 1455 | for (i = 1; i < len; i++) |
| 1456 | args[i - 1] = arguments[i]; |
| 1457 | |
| 1458 | listeners = handler.slice(); |
| 1459 | len = listeners.length; |
| 1460 | for (i = 0; i < len; i++) |
| 1461 | listeners[i].apply(this, args); |
| 1462 | } |
| 1463 | |
| 1464 | return true; |
| 1465 | }; |
| 1466 | |
| 1467 | EventEmitter.prototype.addListener = function(type, listener) { |
| 1468 | var m; |
| 1469 | |
| 1470 | if (!isFunction(listener)) |
| 1471 | throw TypeError('listener must be a function'); |
| 1472 | |
| 1473 | if (!this._events) |
| 1474 | this._events = {}; |
| 1475 | |
| 1476 | // To avoid recursion in the case that type === "newListener"! Before |
| 1477 | // adding it to the listeners, first emit "newListener". |
| 1478 | if (this._events.newListener) |
| 1479 | this.emit('newListener', type, |
| 1480 | isFunction(listener.listener) ? |
| 1481 | listener.listener : listener); |
| 1482 | |
| 1483 | if (!this._events[type]) |
| 1484 | // Optimize the case of one listener. Don't need the extra array object. |
| 1485 | this._events[type] = listener; |
| 1486 | else if (isObject(this._events[type])) |
| 1487 | // If we've already got an array, just append. |
| 1488 | this._events[type].push(listener); |
| 1489 | else |
| 1490 | // Adding the second element, need to change to array. |
| 1491 | this._events[type] = [this._events[type], listener]; |
| 1492 | |
| 1493 | // Check for listener leak |
| 1494 | if (isObject(this._events[type]) && !this._events[type].warned) { |
| 1495 | var m; |
| 1496 | if (!isUndefined(this._maxListeners)) { |
| 1497 | m = this._maxListeners; |
| 1498 | } else { |
| 1499 | m = EventEmitter.defaultMaxListeners; |
| 1500 | } |
| 1501 | |
| 1502 | if (m && m > 0 && this._events[type].length > m) { |
| 1503 | this._events[type].warned = true; |
| 1504 | console.error('(node) warning: possible EventEmitter memory ' + |
| 1505 | 'leak detected. %d listeners added. ' + |
| 1506 | 'Use emitter.setMaxListeners() to increase limit.', |
| 1507 | this._events[type].length); |
| 1508 | if (typeof console.trace === 'function') { |
| 1509 | // not supported in IE 10 |
| 1510 | console.trace(); |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | return this; |
| 1516 | }; |
| 1517 | |
| 1518 | EventEmitter.prototype.on = EventEmitter.prototype.addListener; |
| 1519 | |
| 1520 | EventEmitter.prototype.once = function(type, listener) { |
| 1521 | if (!isFunction(listener)) |
| 1522 | throw TypeError('listener must be a function'); |
| 1523 | |
| 1524 | var fired = false; |
| 1525 | |
| 1526 | function g() { |
| 1527 | this.removeListener(type, g); |
| 1528 | |
| 1529 | if (!fired) { |
| 1530 | fired = true; |
| 1531 | listener.apply(this, arguments); |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | g.listener = listener; |
| 1536 | this.on(type, g); |
| 1537 | |
| 1538 | return this; |
| 1539 | }; |
| 1540 | |
| 1541 | // emits a 'removeListener' event iff the listener was removed |
| 1542 | EventEmitter.prototype.removeListener = function(type, listener) { |
| 1543 | var list, position, length, i; |
| 1544 | |
| 1545 | if (!isFunction(listener)) |
| 1546 | throw TypeError('listener must be a function'); |
| 1547 | |
| 1548 | if (!this._events || !this._events[type]) |
| 1549 | return this; |
| 1550 | |
| 1551 | list = this._events[type]; |
| 1552 | length = list.length; |
| 1553 | position = -1; |
| 1554 | |
| 1555 | if (list === listener || |
| 1556 | (isFunction(list.listener) && list.listener === listener)) { |
| 1557 | delete this._events[type]; |
| 1558 | if (this._events.removeListener) |
| 1559 | this.emit('removeListener', type, listener); |
| 1560 | |
| 1561 | } else if (isObject(list)) { |
| 1562 | for (i = length; i-- > 0;) { |
| 1563 | if (list[i] === listener || |
| 1564 | (list[i].listener && list[i].listener === listener)) { |
| 1565 | position = i; |
| 1566 | break; |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | if (position < 0) |
| 1571 | return this; |
| 1572 | |
| 1573 | if (list.length === 1) { |
| 1574 | list.length = 0; |
| 1575 | delete this._events[type]; |
| 1576 | } else { |
| 1577 | list.splice(position, 1); |
| 1578 | } |
| 1579 | |
| 1580 | if (this._events.removeListener) |
| 1581 | this.emit('removeListener', type, listener); |
| 1582 | } |
| 1583 | |
| 1584 | return this; |
| 1585 | }; |
| 1586 | |
| 1587 | EventEmitter.prototype.removeAllListeners = function(type) { |
| 1588 | var key, listeners; |
| 1589 | |
| 1590 | if (!this._events) |
| 1591 | return this; |
| 1592 | |
| 1593 | // not listening for removeListener, no need to emit |
| 1594 | if (!this._events.removeListener) { |
| 1595 | if (arguments.length === 0) |
| 1596 | this._events = {}; |
| 1597 | else if (this._events[type]) |
| 1598 | delete this._events[type]; |
| 1599 | return this; |
| 1600 | } |
| 1601 | |
| 1602 | // emit removeListener for all listeners on all events |
| 1603 | if (arguments.length === 0) { |
| 1604 | for (key in this._events) { |
| 1605 | if (key === 'removeListener') continue; |
| 1606 | this.removeAllListeners(key); |
| 1607 | } |
| 1608 | this.removeAllListeners('removeListener'); |
| 1609 | this._events = {}; |
| 1610 | return this; |
| 1611 | } |
| 1612 | |
| 1613 | listeners = this._events[type]; |
| 1614 | |
| 1615 | if (isFunction(listeners)) { |
| 1616 | this.removeListener(type, listeners); |
| 1617 | } else { |
| 1618 | // LIFO order |
| 1619 | while (listeners.length) |
| 1620 | this.removeListener(type, listeners[listeners.length - 1]); |
| 1621 | } |
| 1622 | delete this._events[type]; |
| 1623 | |
| 1624 | return this; |
| 1625 | }; |
| 1626 | |
| 1627 | EventEmitter.prototype.listeners = function(type) { |
| 1628 | var ret; |
| 1629 | if (!this._events || !this._events[type]) |
| 1630 | ret = []; |
| 1631 | else if (isFunction(this._events[type])) |
| 1632 | ret = [this._events[type]]; |
| 1633 | else |
| 1634 | ret = this._events[type].slice(); |
| 1635 | return ret; |
| 1636 | }; |
| 1637 | |
| 1638 | EventEmitter.listenerCount = function(emitter, type) { |
| 1639 | var ret; |
| 1640 | if (!emitter._events || !emitter._events[type]) |
| 1641 | ret = 0; |
| 1642 | else if (isFunction(emitter._events[type])) |
| 1643 | ret = 1; |
| 1644 | else |
| 1645 | ret = emitter._events[type].length; |
| 1646 | return ret; |
| 1647 | }; |
| 1648 | |
| 1649 | function isFunction(arg) { |
| 1650 | return typeof arg === 'function'; |
| 1651 | } |
| 1652 | |
| 1653 | function isNumber(arg) { |
| 1654 | return typeof arg === 'number'; |
| 1655 | } |
| 1656 | |
| 1657 | function isObject(arg) { |
| 1658 | return typeof arg === 'object' && arg !== null; |
| 1659 | } |
| 1660 | |
| 1661 | function isUndefined(arg) { |
| 1662 | return arg === void 0; |
| 1663 | } |
| 1664 | |
| 1665 | },{}],5:[function(require,module,exports){ |
| 1666 | if (typeof Object.create === 'function') { |
| 1667 | // implementation from standard node.js 'util' module |
| 1668 | module.exports = function inherits(ctor, superCtor) { |
| 1669 | ctor.super_ = superCtor |
| 1670 | ctor.prototype = Object.create(superCtor.prototype, { |
| 1671 | constructor: { |
| 1672 | value: ctor, |
| 1673 | enumerable: false, |
| 1674 | writable: true, |
| 1675 | configurable: true |
| 1676 | } |
| 1677 | }); |
| 1678 | }; |
| 1679 | } else { |
| 1680 | // old school shim for old browsers |
| 1681 | module.exports = function inherits(ctor, superCtor) { |
| 1682 | ctor.super_ = superCtor |
| 1683 | var TempCtor = function () {} |
| 1684 | TempCtor.prototype = superCtor.prototype |
| 1685 | ctor.prototype = new TempCtor() |
| 1686 | ctor.prototype.constructor = ctor |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | },{}],6:[function(require,module,exports){ |
| 1691 | // shim for using process in browser |
| 1692 | |
| 1693 | var process = module.exports = {}; |
| 1694 | |
| 1695 | process.nextTick = (function () { |
| 1696 | var canSetImmediate = typeof window !== 'undefined' |
| 1697 | && window.setImmediate; |
| 1698 | var canPost = typeof window !== 'undefined' |
| 1699 | && window.postMessage && window.addEventListener |
| 1700 | ; |
| 1701 | |
| 1702 | if (canSetImmediate) { |
| 1703 | return function (f) { return window.setImmediate(f) }; |
| 1704 | } |
| 1705 | |
| 1706 | if (canPost) { |
| 1707 | var queue = []; |
| 1708 | window.addEventListener('message', function (ev) { |
| 1709 | var source = ev.source; |
| 1710 | if ((source === window || source === null) && ev.data === 'process-tick') { |
| 1711 | ev.stopPropagation(); |
| 1712 | if (queue.length > 0) { |
| 1713 | var fn = queue.shift(); |
| 1714 | fn(); |
| 1715 | } |
| 1716 | } |
| 1717 | }, true); |
| 1718 | |
| 1719 | return function nextTick(fn) { |
| 1720 | queue.push(fn); |
| 1721 | window.postMessage('process-tick', '*'); |
| 1722 | }; |
| 1723 | } |
| 1724 | |
| 1725 | return function nextTick(fn) { |
| 1726 | setTimeout(fn, 0); |
| 1727 | }; |
| 1728 | })(); |
| 1729 | |
| 1730 | process.title = 'browser'; |
| 1731 | process.browser = true; |
| 1732 | process.env = {}; |
| 1733 | process.argv = []; |
| 1734 | |
| 1735 | function noop() {} |
| 1736 | |
| 1737 | process.on = noop; |
| 1738 | process.addListener = noop; |
| 1739 | process.once = noop; |
| 1740 | process.off = noop; |
| 1741 | process.removeListener = noop; |
| 1742 | process.removeAllListeners = noop; |
| 1743 | process.emit = noop; |
| 1744 | |
| 1745 | process.binding = function (name) { |
| 1746 | throw new Error('process.binding is not supported'); |
| 1747 | } |
| 1748 | |
| 1749 | // TODO(shtylman) |
| 1750 | process.cwd = function () { return '/' }; |
| 1751 | process.chdir = function (dir) { |
| 1752 | throw new Error('process.chdir is not supported'); |
| 1753 | }; |
| 1754 | |
| 1755 | },{}],7:[function(require,module,exports){ |
| 1756 | module.exports = require("./lib/_stream_duplex.js") |
| 1757 | |
| 1758 | },{"./lib/_stream_duplex.js":8}],8:[function(require,module,exports){ |
| 1759 | (function (process){ |
| 1760 | // Copyright Joyent, Inc. and other Node contributors. |
| 1761 | // |
| 1762 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 1763 | // copy of this software and associated documentation files (the |
| 1764 | // "Software"), to deal in the Software without restriction, including |
| 1765 | // without limitation the rights to use, copy, modify, merge, publish, |
| 1766 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 1767 | // persons to whom the Software is furnished to do so, subject to the |
| 1768 | // following conditions: |
| 1769 | // |
| 1770 | // The above copyright notice and this permission notice shall be included |
| 1771 | // in all copies or substantial portions of the Software. |
| 1772 | // |
| 1773 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 1774 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 1775 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 1776 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 1777 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 1778 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 1779 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 1780 | |
| 1781 | // a duplex stream is just a stream that is both readable and writable. |
| 1782 | // Since JS doesn't have multiple prototypal inheritance, this class |
| 1783 | // prototypally inherits from Readable, and then parasitically from |
| 1784 | // Writable. |
| 1785 | |
| 1786 | module.exports = Duplex; |
| 1787 | |
| 1788 | /*<replacement>*/ |
| 1789 | var objectKeys = Object.keys || function (obj) { |
| 1790 | var keys = []; |
| 1791 | for (var key in obj) keys.push(key); |
| 1792 | return keys; |
| 1793 | } |
| 1794 | /*</replacement>*/ |
| 1795 | |
| 1796 | |
| 1797 | /*<replacement>*/ |
| 1798 | var util = require('core-util-is'); |
| 1799 | util.inherits = require('inherits'); |
| 1800 | /*</replacement>*/ |
| 1801 | |
| 1802 | var Readable = require('./_stream_readable'); |
| 1803 | var Writable = require('./_stream_writable'); |
| 1804 | |
| 1805 | util.inherits(Duplex, Readable); |
| 1806 | |
| 1807 | forEach(objectKeys(Writable.prototype), function(method) { |
| 1808 | if (!Duplex.prototype[method]) |
| 1809 | Duplex.prototype[method] = Writable.prototype[method]; |
| 1810 | }); |
| 1811 | |
| 1812 | function Duplex(options) { |
| 1813 | if (!(this instanceof Duplex)) |
| 1814 | return new Duplex(options); |
| 1815 | |
| 1816 | Readable.call(this, options); |
| 1817 | Writable.call(this, options); |
| 1818 | |
| 1819 | if (options && options.readable === false) |
| 1820 | this.readable = false; |
| 1821 | |
| 1822 | if (options && options.writable === false) |
| 1823 | this.writable = false; |
| 1824 | |
| 1825 | this.allowHalfOpen = true; |
| 1826 | if (options && options.allowHalfOpen === false) |
| 1827 | this.allowHalfOpen = false; |
| 1828 | |
| 1829 | this.once('end', onend); |
| 1830 | } |
| 1831 | |
| 1832 | // the no-half-open enforcer |
| 1833 | function onend() { |
| 1834 | // if we allow half-open state, or if the writable side ended, |
| 1835 | // then we're ok. |
| 1836 | if (this.allowHalfOpen || this._writableState.ended) |
| 1837 | return; |
| 1838 | |
| 1839 | // no more data can be written. |
| 1840 | // But allow more writes to happen in this tick. |
| 1841 | process.nextTick(this.end.bind(this)); |
| 1842 | } |
| 1843 | |
| 1844 | function forEach (xs, f) { |
| 1845 | for (var i = 0, l = xs.length; i < l; i++) { |
| 1846 | f(xs[i], i); |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | }).call(this,require("4dON6Z")) |
| 1851 | },{"./_stream_readable":10,"./_stream_writable":12,"4dON6Z":6,"core-util-is":13,"inherits":5}],9:[function(require,module,exports){ |
| 1852 | // Copyright Joyent, Inc. and other Node contributors. |
| 1853 | // |
| 1854 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 1855 | // copy of this software and associated documentation files (the |
| 1856 | // "Software"), to deal in the Software without restriction, including |
| 1857 | // without limitation the rights to use, copy, modify, merge, publish, |
| 1858 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 1859 | // persons to whom the Software is furnished to do so, subject to the |
| 1860 | // following conditions: |
| 1861 | // |
| 1862 | // The above copyright notice and this permission notice shall be included |
| 1863 | // in all copies or substantial portions of the Software. |
| 1864 | // |
| 1865 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 1866 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 1867 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 1868 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 1869 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 1870 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 1871 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 1872 | |
| 1873 | // a passthrough stream. |
| 1874 | // basically just the most minimal sort of Transform stream. |
| 1875 | // Every written chunk gets output as-is. |
| 1876 | |
| 1877 | module.exports = PassThrough; |
| 1878 | |
| 1879 | var Transform = require('./_stream_transform'); |
| 1880 | |
| 1881 | /*<replacement>*/ |
| 1882 | var util = require('core-util-is'); |
| 1883 | util.inherits = require('inherits'); |
| 1884 | /*</replacement>*/ |
| 1885 | |
| 1886 | util.inherits(PassThrough, Transform); |
| 1887 | |
| 1888 | function PassThrough(options) { |
| 1889 | if (!(this instanceof PassThrough)) |
| 1890 | return new PassThrough(options); |
| 1891 | |
| 1892 | Transform.call(this, options); |
| 1893 | } |
| 1894 | |
| 1895 | PassThrough.prototype._transform = function(chunk, encoding, cb) { |
| 1896 | cb(null, chunk); |
| 1897 | }; |
| 1898 | |
| 1899 | },{"./_stream_transform":11,"core-util-is":13,"inherits":5}],10:[function(require,module,exports){ |
| 1900 | (function (process){ |
| 1901 | // Copyright Joyent, Inc. and other Node contributors. |
| 1902 | // |
| 1903 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 1904 | // copy of this software and associated documentation files (the |
| 1905 | // "Software"), to deal in the Software without restriction, including |
| 1906 | // without limitation the rights to use, copy, modify, merge, publish, |
| 1907 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 1908 | // persons to whom the Software is furnished to do so, subject to the |
| 1909 | // following conditions: |
| 1910 | // |
| 1911 | // The above copyright notice and this permission notice shall be included |
| 1912 | // in all copies or substantial portions of the Software. |
| 1913 | // |
| 1914 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 1915 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 1916 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 1917 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 1918 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 1919 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 1920 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 1921 | |
| 1922 | module.exports = Readable; |
| 1923 | |
| 1924 | /*<replacement>*/ |
| 1925 | var isArray = require('isarray'); |
| 1926 | /*</replacement>*/ |
| 1927 | |
| 1928 | |
| 1929 | /*<replacement>*/ |
| 1930 | var Buffer = require('buffer').Buffer; |
| 1931 | /*</replacement>*/ |
| 1932 | |
| 1933 | Readable.ReadableState = ReadableState; |
| 1934 | |
| 1935 | var EE = require('events').EventEmitter; |
| 1936 | |
| 1937 | /*<replacement>*/ |
| 1938 | if (!EE.listenerCount) EE.listenerCount = function(emitter, type) { |
| 1939 | return emitter.listeners(type).length; |
| 1940 | }; |
| 1941 | /*</replacement>*/ |
| 1942 | |
| 1943 | var Stream = require('stream'); |
| 1944 | |
| 1945 | /*<replacement>*/ |
| 1946 | var util = require('core-util-is'); |
| 1947 | util.inherits = require('inherits'); |
| 1948 | /*</replacement>*/ |
| 1949 | |
| 1950 | var StringDecoder; |
| 1951 | |
| 1952 | util.inherits(Readable, Stream); |
| 1953 | |
| 1954 | function ReadableState(options, stream) { |
| 1955 | options = options || {}; |
| 1956 | |
| 1957 | // the point at which it stops calling _read() to fill the buffer |
| 1958 | // Note: 0 is a valid value, means "don't call _read preemptively ever" |
| 1959 | var hwm = options.highWaterMark; |
| 1960 | this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024; |
| 1961 | |
| 1962 | // cast to ints. |
| 1963 | this.highWaterMark = ~~this.highWaterMark; |
| 1964 | |
| 1965 | this.buffer = []; |
| 1966 | this.length = 0; |
| 1967 | this.pipes = null; |
| 1968 | this.pipesCount = 0; |
| 1969 | this.flowing = false; |
| 1970 | this.ended = false; |
| 1971 | this.endEmitted = false; |
| 1972 | this.reading = false; |
| 1973 | |
| 1974 | // In streams that never have any data, and do push(null) right away, |
| 1975 | // the consumer can miss the 'end' event if they do some I/O before |
| 1976 | // consuming the stream. So, we don't emit('end') until some reading |
| 1977 | // happens. |
| 1978 | this.calledRead = false; |
| 1979 | |
| 1980 | // a flag to be able to tell if the onwrite cb is called immediately, |
| 1981 | // or on a later tick. We set this to true at first, becuase any |
| 1982 | // actions that shouldn't happen until "later" should generally also |
| 1983 | // not happen before the first write call. |
| 1984 | this.sync = true; |
| 1985 | |
| 1986 | // whenever we return null, then we set a flag to say |
| 1987 | // that we're awaiting a 'readable' event emission. |
| 1988 | this.needReadable = false; |
| 1989 | this.emittedReadable = false; |
| 1990 | this.readableListening = false; |
| 1991 | |
| 1992 | |
| 1993 | // object stream flag. Used to make read(n) ignore n and to |
| 1994 | // make all the buffer merging and length checks go away |
| 1995 | this.objectMode = !!options.objectMode; |
| 1996 | |
| 1997 | // Crypto is kind of old and crusty. Historically, its default string |
| 1998 | // encoding is 'binary' so we have to make this configurable. |
| 1999 | // Everything else in the universe uses 'utf8', though. |
| 2000 | this.defaultEncoding = options.defaultEncoding || 'utf8'; |
| 2001 | |
| 2002 | // when piping, we only care about 'readable' events that happen |
| 2003 | // after read()ing all the bytes and not getting any pushback. |
| 2004 | this.ranOut = false; |
| 2005 | |
| 2006 | // the number of writers that are awaiting a drain event in .pipe()s |
| 2007 | this.awaitDrain = 0; |
| 2008 | |
| 2009 | // if true, a maybeReadMore has been scheduled |
| 2010 | this.readingMore = false; |
| 2011 | |
| 2012 | this.decoder = null; |
| 2013 | this.encoding = null; |
| 2014 | if (options.encoding) { |
| 2015 | if (!StringDecoder) |
| 2016 | StringDecoder = require('string_decoder/').StringDecoder; |
| 2017 | this.decoder = new StringDecoder(options.encoding); |
| 2018 | this.encoding = options.encoding; |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | function Readable(options) { |
| 2023 | if (!(this instanceof Readable)) |
| 2024 | return new Readable(options); |
| 2025 | |
| 2026 | this._readableState = new ReadableState(options, this); |
| 2027 | |
| 2028 | // legacy |
| 2029 | this.readable = true; |
| 2030 | |
| 2031 | Stream.call(this); |
| 2032 | } |
| 2033 | |
| 2034 | // Manually shove something into the read() buffer. |
| 2035 | // This returns true if the highWaterMark has not been hit yet, |
| 2036 | // similar to how Writable.write() returns true if you should |
| 2037 | // write() some more. |
| 2038 | Readable.prototype.push = function(chunk, encoding) { |
| 2039 | var state = this._readableState; |
| 2040 | |
| 2041 | if (typeof chunk === 'string' && !state.objectMode) { |
| 2042 | encoding = encoding || state.defaultEncoding; |
| 2043 | if (encoding !== state.encoding) { |
| 2044 | chunk = new Buffer(chunk, encoding); |
| 2045 | encoding = ''; |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | return readableAddChunk(this, state, chunk, encoding, false); |
| 2050 | }; |
| 2051 | |
| 2052 | // Unshift should *always* be something directly out of read() |
| 2053 | Readable.prototype.unshift = function(chunk) { |
| 2054 | var state = this._readableState; |
| 2055 | return readableAddChunk(this, state, chunk, '', true); |
| 2056 | }; |
| 2057 | |
| 2058 | function readableAddChunk(stream, state, chunk, encoding, addToFront) { |
| 2059 | var er = chunkInvalid(state, chunk); |
| 2060 | if (er) { |
| 2061 | stream.emit('error', er); |
| 2062 | } else if (chunk === null || chunk === undefined) { |
| 2063 | state.reading = false; |
| 2064 | if (!state.ended) |
| 2065 | onEofChunk(stream, state); |
| 2066 | } else if (state.objectMode || chunk && chunk.length > 0) { |
| 2067 | if (state.ended && !addToFront) { |
| 2068 | var e = new Error('stream.push() after EOF'); |
| 2069 | stream.emit('error', e); |
| 2070 | } else if (state.endEmitted && addToFront) { |
| 2071 | var e = new Error('stream.unshift() after end event'); |
| 2072 | stream.emit('error', e); |
| 2073 | } else { |
| 2074 | if (state.decoder && !addToFront && !encoding) |
| 2075 | chunk = state.decoder.write(chunk); |
| 2076 | |
| 2077 | // update the buffer info. |
| 2078 | state.length += state.objectMode ? 1 : chunk.length; |
| 2079 | if (addToFront) { |
| 2080 | state.buffer.unshift(chunk); |
| 2081 | } else { |
| 2082 | state.reading = false; |
| 2083 | state.buffer.push(chunk); |
| 2084 | } |
| 2085 | |
| 2086 | if (state.needReadable) |
| 2087 | emitReadable(stream); |
| 2088 | |
| 2089 | maybeReadMore(stream, state); |
| 2090 | } |
| 2091 | } else if (!addToFront) { |
| 2092 | state.reading = false; |
| 2093 | } |
| 2094 | |
| 2095 | return needMoreData(state); |
| 2096 | } |
| 2097 | |
| 2098 | |
| 2099 | |
| 2100 | // if it's past the high water mark, we can push in some more. |
| 2101 | // Also, if we have no data yet, we can stand some |
| 2102 | // more bytes. This is to work around cases where hwm=0, |
| 2103 | // such as the repl. Also, if the push() triggered a |
| 2104 | // readable event, and the user called read(largeNumber) such that |
| 2105 | // needReadable was set, then we ought to push more, so that another |
| 2106 | // 'readable' event will be triggered. |
| 2107 | function needMoreData(state) { |
| 2108 | return !state.ended && |
| 2109 | (state.needReadable || |
| 2110 | state.length < state.highWaterMark || |
| 2111 | state.length === 0); |
| 2112 | } |
| 2113 | |
| 2114 | // backwards compatibility. |
| 2115 | Readable.prototype.setEncoding = function(enc) { |
| 2116 | if (!StringDecoder) |
| 2117 | StringDecoder = require('string_decoder/').StringDecoder; |
| 2118 | this._readableState.decoder = new StringDecoder(enc); |
| 2119 | this._readableState.encoding = enc; |
| 2120 | }; |
| 2121 | |
| 2122 | // Don't raise the hwm > 128MB |
| 2123 | var MAX_HWM = 0x800000; |
| 2124 | function roundUpToNextPowerOf2(n) { |
| 2125 | if (n >= MAX_HWM) { |
| 2126 | n = MAX_HWM; |
| 2127 | } else { |
| 2128 | // Get the next highest power of 2 |
| 2129 | n--; |
| 2130 | for (var p = 1; p < 32; p <<= 1) n |= n >> p; |
| 2131 | n++; |
| 2132 | } |
| 2133 | return n; |
| 2134 | } |
| 2135 | |
| 2136 | function howMuchToRead(n, state) { |
| 2137 | if (state.length === 0 && state.ended) |
| 2138 | return 0; |
| 2139 | |
| 2140 | if (state.objectMode) |
| 2141 | return n === 0 ? 0 : 1; |
| 2142 | |
| 2143 | if (isNaN(n) || n === null) { |
| 2144 | // only flow one buffer at a time |
| 2145 | if (state.flowing && state.buffer.length) |
| 2146 | return state.buffer[0].length; |
| 2147 | else |
| 2148 | return state.length; |
| 2149 | } |
| 2150 | |
| 2151 | if (n <= 0) |
| 2152 | return 0; |
| 2153 | |
| 2154 | // If we're asking for more than the target buffer level, |
| 2155 | // then raise the water mark. Bump up to the next highest |
| 2156 | // power of 2, to prevent increasing it excessively in tiny |
| 2157 | // amounts. |
| 2158 | if (n > state.highWaterMark) |
| 2159 | state.highWaterMark = roundUpToNextPowerOf2(n); |
| 2160 | |
| 2161 | // don't have that much. return null, unless we've ended. |
| 2162 | if (n > state.length) { |
| 2163 | if (!state.ended) { |
| 2164 | state.needReadable = true; |
| 2165 | return 0; |
| 2166 | } else |
| 2167 | return state.length; |
| 2168 | } |
| 2169 | |
| 2170 | return n; |
| 2171 | } |
| 2172 | |
| 2173 | // you can override either this method, or the async _read(n) below. |
| 2174 | Readable.prototype.read = function(n) { |
| 2175 | var state = this._readableState; |
| 2176 | state.calledRead = true; |
| 2177 | var nOrig = n; |
| 2178 | |
| 2179 | if (typeof n !== 'number' || n > 0) |
| 2180 | state.emittedReadable = false; |
| 2181 | |
| 2182 | // if we're doing read(0) to trigger a readable event, but we |
| 2183 | // already have a bunch of data in the buffer, then just trigger |
| 2184 | // the 'readable' event and move on. |
| 2185 | if (n === 0 && |
| 2186 | state.needReadable && |
| 2187 | (state.length >= state.highWaterMark || state.ended)) { |
| 2188 | emitReadable(this); |
| 2189 | return null; |
| 2190 | } |
| 2191 | |
| 2192 | n = howMuchToRead(n, state); |
| 2193 | |
| 2194 | // if we've ended, and we're now clear, then finish it up. |
| 2195 | if (n === 0 && state.ended) { |
| 2196 | if (state.length === 0) |
| 2197 | endReadable(this); |
| 2198 | return null; |
| 2199 | } |
| 2200 | |
| 2201 | // All the actual chunk generation logic needs to be |
| 2202 | // *below* the call to _read. The reason is that in certain |
| 2203 | // synthetic stream cases, such as passthrough streams, _read |
| 2204 | // may be a completely synchronous operation which may change |
| 2205 | // the state of the read buffer, providing enough data when |
| 2206 | // before there was *not* enough. |
| 2207 | // |
| 2208 | // So, the steps are: |
| 2209 | // 1. Figure out what the state of things will be after we do |
| 2210 | // a read from the buffer. |
| 2211 | // |
| 2212 | // 2. If that resulting state will trigger a _read, then call _read. |
| 2213 | // Note that this may be asynchronous, or synchronous. Yes, it is |
| 2214 | // deeply ugly to write APIs this way, but that still doesn't mean |
| 2215 | // that the Readable class should behave improperly, as streams are |
| 2216 | // designed to be sync/async agnostic. |
| 2217 | // Take note if the _read call is sync or async (ie, if the read call |
| 2218 | // has returned yet), so that we know whether or not it's safe to emit |
| 2219 | // 'readable' etc. |
| 2220 | // |
| 2221 | // 3. Actually pull the requested chunks out of the buffer and return. |
| 2222 | |
| 2223 | // if we need a readable event, then we need to do some reading. |
| 2224 | var doRead = state.needReadable; |
| 2225 | |
| 2226 | // if we currently have less than the highWaterMark, then also read some |
| 2227 | if (state.length - n <= state.highWaterMark) |
| 2228 | doRead = true; |
| 2229 | |
| 2230 | // however, if we've ended, then there's no point, and if we're already |
| 2231 | // reading, then it's unnecessary. |
| 2232 | if (state.ended || state.reading) |
| 2233 | doRead = false; |
| 2234 | |
| 2235 | if (doRead) { |
| 2236 | state.reading = true; |
| 2237 | state.sync = true; |
| 2238 | // if the length is currently zero, then we *need* a readable event. |
| 2239 | if (state.length === 0) |
| 2240 | state.needReadable = true; |
| 2241 | // call internal read method |
| 2242 | this._read(state.highWaterMark); |
| 2243 | state.sync = false; |
| 2244 | } |
| 2245 | |
| 2246 | // If _read called its callback synchronously, then `reading` |
| 2247 | // will be false, and we need to re-evaluate how much data we |
| 2248 | // can return to the user. |
| 2249 | if (doRead && !state.reading) |
| 2250 | n = howMuchToRead(nOrig, state); |
| 2251 | |
| 2252 | var ret; |
| 2253 | if (n > 0) |
| 2254 | ret = fromList(n, state); |
| 2255 | else |
| 2256 | ret = null; |
| 2257 | |
| 2258 | if (ret === null) { |
| 2259 | state.needReadable = true; |
| 2260 | n = 0; |
| 2261 | } |
| 2262 | |
| 2263 | state.length -= n; |
| 2264 | |
| 2265 | // If we have nothing in the buffer, then we want to know |
| 2266 | // as soon as we *do* get something into the buffer. |
| 2267 | if (state.length === 0 && !state.ended) |
| 2268 | state.needReadable = true; |
| 2269 | |
| 2270 | // If we happened to read() exactly the remaining amount in the |
| 2271 | // buffer, and the EOF has been seen at this point, then make sure |
| 2272 | // that we emit 'end' on the very next tick. |
| 2273 | if (state.ended && !state.endEmitted && state.length === 0) |
| 2274 | endReadable(this); |
| 2275 | |
| 2276 | return ret; |
| 2277 | }; |
| 2278 | |
| 2279 | function chunkInvalid(state, chunk) { |
| 2280 | var er = null; |
| 2281 | if (!Buffer.isBuffer(chunk) && |
| 2282 | 'string' !== typeof chunk && |
| 2283 | chunk !== null && |
| 2284 | chunk !== undefined && |
| 2285 | !state.objectMode && |
| 2286 | !er) { |
| 2287 | er = new TypeError('Invalid non-string/buffer chunk'); |
| 2288 | } |
| 2289 | return er; |
| 2290 | } |
| 2291 | |
| 2292 | |
| 2293 | function onEofChunk(stream, state) { |
| 2294 | if (state.decoder && !state.ended) { |
| 2295 | var chunk = state.decoder.end(); |
| 2296 | if (chunk && chunk.length) { |
| 2297 | state.buffer.push(chunk); |
| 2298 | state.length += state.objectMode ? 1 : chunk.length; |
| 2299 | } |
| 2300 | } |
| 2301 | state.ended = true; |
| 2302 | |
| 2303 | // if we've ended and we have some data left, then emit |
| 2304 | // 'readable' now to make sure it gets picked up. |
| 2305 | if (state.length > 0) |
| 2306 | emitReadable(stream); |
| 2307 | else |
| 2308 | endReadable(stream); |
| 2309 | } |
| 2310 | |
| 2311 | // Don't emit readable right away in sync mode, because this can trigger |
| 2312 | // another read() call => stack overflow. This way, it might trigger |
| 2313 | // a nextTick recursion warning, but that's not so bad. |
| 2314 | function emitReadable(stream) { |
| 2315 | var state = stream._readableState; |
| 2316 | state.needReadable = false; |
| 2317 | if (state.emittedReadable) |
| 2318 | return; |
| 2319 | |
| 2320 | state.emittedReadable = true; |
| 2321 | if (state.sync) |
| 2322 | process.nextTick(function() { |
| 2323 | emitReadable_(stream); |
| 2324 | }); |
| 2325 | else |
| 2326 | emitReadable_(stream); |
| 2327 | } |
| 2328 | |
| 2329 | function emitReadable_(stream) { |
| 2330 | stream.emit('readable'); |
| 2331 | } |
| 2332 | |
| 2333 | |
| 2334 | // at this point, the user has presumably seen the 'readable' event, |
| 2335 | // and called read() to consume some data. that may have triggered |
| 2336 | // in turn another _read(n) call, in which case reading = true if |
| 2337 | // it's in progress. |
| 2338 | // However, if we're not ended, or reading, and the length < hwm, |
| 2339 | // then go ahead and try to read some more preemptively. |
| 2340 | function maybeReadMore(stream, state) { |
| 2341 | if (!state.readingMore) { |
| 2342 | state.readingMore = true; |
| 2343 | process.nextTick(function() { |
| 2344 | maybeReadMore_(stream, state); |
| 2345 | }); |
| 2346 | } |
| 2347 | } |
| 2348 | |
| 2349 | function maybeReadMore_(stream, state) { |
| 2350 | var len = state.length; |
| 2351 | while (!state.reading && !state.flowing && !state.ended && |
| 2352 | state.length < state.highWaterMark) { |
| 2353 | stream.read(0); |
| 2354 | if (len === state.length) |
| 2355 | // didn't get any data, stop spinning. |
| 2356 | break; |
| 2357 | else |
| 2358 | len = state.length; |
| 2359 | } |
| 2360 | state.readingMore = false; |
| 2361 | } |
| 2362 | |
| 2363 | // abstract method. to be overridden in specific implementation classes. |
| 2364 | // call cb(er, data) where data is <= n in length. |
| 2365 | // for virtual (non-string, non-buffer) streams, "length" is somewhat |
| 2366 | // arbitrary, and perhaps not very meaningful. |
| 2367 | Readable.prototype._read = function(n) { |
| 2368 | this.emit('error', new Error('not implemented')); |
| 2369 | }; |
| 2370 | |
| 2371 | Readable.prototype.pipe = function(dest, pipeOpts) { |
| 2372 | var src = this; |
| 2373 | var state = this._readableState; |
| 2374 | |
| 2375 | switch (state.pipesCount) { |
| 2376 | case 0: |
| 2377 | state.pipes = dest; |
| 2378 | break; |
| 2379 | case 1: |
| 2380 | state.pipes = [state.pipes, dest]; |
| 2381 | break; |
| 2382 | default: |
| 2383 | state.pipes.push(dest); |
| 2384 | break; |
| 2385 | } |
| 2386 | state.pipesCount += 1; |
| 2387 | |
| 2388 | var doEnd = (!pipeOpts || pipeOpts.end !== false) && |
| 2389 | dest !== process.stdout && |
| 2390 | dest !== process.stderr; |
| 2391 | |
| 2392 | var endFn = doEnd ? onend : cleanup; |
| 2393 | if (state.endEmitted) |
| 2394 | process.nextTick(endFn); |
| 2395 | else |
| 2396 | src.once('end', endFn); |
| 2397 | |
| 2398 | dest.on('unpipe', onunpipe); |
| 2399 | function onunpipe(readable) { |
| 2400 | if (readable !== src) return; |
| 2401 | cleanup(); |
| 2402 | } |
| 2403 | |
| 2404 | function onend() { |
| 2405 | dest.end(); |
| 2406 | } |
| 2407 | |
| 2408 | // when the dest drains, it reduces the awaitDrain counter |
| 2409 | // on the source. This would be more elegant with a .once() |
| 2410 | // handler in flow(), but adding and removing repeatedly is |
| 2411 | // too slow. |
| 2412 | var ondrain = pipeOnDrain(src); |
| 2413 | dest.on('drain', ondrain); |
| 2414 | |
| 2415 | function cleanup() { |
| 2416 | // cleanup event handlers once the pipe is broken |
| 2417 | dest.removeListener('close', onclose); |
| 2418 | dest.removeListener('finish', onfinish); |
| 2419 | dest.removeListener('drain', ondrain); |
| 2420 | dest.removeListener('error', onerror); |
| 2421 | dest.removeListener('unpipe', onunpipe); |
| 2422 | src.removeListener('end', onend); |
| 2423 | src.removeListener('end', cleanup); |
| 2424 | |
| 2425 | // if the reader is waiting for a drain event from this |
| 2426 | // specific writer, then it would cause it to never start |
| 2427 | // flowing again. |
| 2428 | // So, if this is awaiting a drain, then we just call it now. |
| 2429 | // If we don't know, then assume that we are waiting for one. |
| 2430 | if (!dest._writableState || dest._writableState.needDrain) |
| 2431 | ondrain(); |
| 2432 | } |
| 2433 | |
| 2434 | // if the dest has an error, then stop piping into it. |
| 2435 | // however, don't suppress the throwing behavior for this. |
| 2436 | function onerror(er) { |
| 2437 | unpipe(); |
| 2438 | dest.removeListener('error', onerror); |
| 2439 | if (EE.listenerCount(dest, 'error') === 0) |
| 2440 | dest.emit('error', er); |
| 2441 | } |
| 2442 | // This is a brutally ugly hack to make sure that our error handler |
| 2443 | // is attached before any userland ones. NEVER DO THIS. |
| 2444 | if (!dest._events || !dest._events.error) |
| 2445 | dest.on('error', onerror); |
| 2446 | else if (isArray(dest._events.error)) |
| 2447 | dest._events.error.unshift(onerror); |
| 2448 | else |
| 2449 | dest._events.error = [onerror, dest._events.error]; |
| 2450 | |
| 2451 | |
| 2452 | |
| 2453 | // Both close and finish should trigger unpipe, but only once. |
| 2454 | function onclose() { |
| 2455 | dest.removeListener('finish', onfinish); |
| 2456 | unpipe(); |
| 2457 | } |
| 2458 | dest.once('close', onclose); |
| 2459 | function onfinish() { |
| 2460 | dest.removeListener('close', onclose); |
| 2461 | unpipe(); |
| 2462 | } |
| 2463 | dest.once('finish', onfinish); |
| 2464 | |
| 2465 | function unpipe() { |
| 2466 | src.unpipe(dest); |
| 2467 | } |
| 2468 | |
| 2469 | // tell the dest that it's being piped to |
| 2470 | dest.emit('pipe', src); |
| 2471 | |
| 2472 | // start the flow if it hasn't been started already. |
| 2473 | if (!state.flowing) { |
| 2474 | // the handler that waits for readable events after all |
| 2475 | // the data gets sucked out in flow. |
| 2476 | // This would be easier to follow with a .once() handler |
| 2477 | // in flow(), but that is too slow. |
| 2478 | this.on('readable', pipeOnReadable); |
| 2479 | |
| 2480 | state.flowing = true; |
| 2481 | process.nextTick(function() { |
| 2482 | flow(src); |
| 2483 | }); |
| 2484 | } |
| 2485 | |
| 2486 | return dest; |
| 2487 | }; |
| 2488 | |
| 2489 | function pipeOnDrain(src) { |
| 2490 | return function() { |
| 2491 | var dest = this; |
| 2492 | var state = src._readableState; |
| 2493 | state.awaitDrain--; |
| 2494 | if (state.awaitDrain === 0) |
| 2495 | flow(src); |
| 2496 | }; |
| 2497 | } |
| 2498 | |
| 2499 | function flow(src) { |
| 2500 | var state = src._readableState; |
| 2501 | var chunk; |
| 2502 | state.awaitDrain = 0; |
| 2503 | |
| 2504 | function write(dest, i, list) { |
| 2505 | var written = dest.write(chunk); |
| 2506 | if (false === written) { |
| 2507 | state.awaitDrain++; |
| 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | while (state.pipesCount && null !== (chunk = src.read())) { |
| 2512 | |
| 2513 | if (state.pipesCount === 1) |
| 2514 | write(state.pipes, 0, null); |
| 2515 | else |
| 2516 | forEach(state.pipes, write); |
| 2517 | |
| 2518 | src.emit('data', chunk); |
| 2519 | |
| 2520 | // if anyone needs a drain, then we have to wait for that. |
| 2521 | if (state.awaitDrain > 0) |
| 2522 | return; |
| 2523 | } |
| 2524 | |
| 2525 | // if every destination was unpiped, either before entering this |
| 2526 | // function, or in the while loop, then stop flowing. |
| 2527 | // |
| 2528 | // NB: This is a pretty rare edge case. |
| 2529 | if (state.pipesCount === 0) { |
| 2530 | state.flowing = false; |
| 2531 | |
| 2532 | // if there were data event listeners added, then switch to old mode. |
| 2533 | if (EE.listenerCount(src, 'data') > 0) |
| 2534 | emitDataEvents(src); |
| 2535 | return; |
| 2536 | } |
| 2537 | |
| 2538 | // at this point, no one needed a drain, so we just ran out of data |
| 2539 | // on the next readable event, start it over again. |
| 2540 | state.ranOut = true; |
| 2541 | } |
| 2542 | |
| 2543 | function pipeOnReadable() { |
| 2544 | if (this._readableState.ranOut) { |
| 2545 | this._readableState.ranOut = false; |
| 2546 | flow(this); |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | |
| 2551 | Readable.prototype.unpipe = function(dest) { |
| 2552 | var state = this._readableState; |
| 2553 | |
| 2554 | // if we're not piping anywhere, then do nothing. |
| 2555 | if (state.pipesCount === 0) |
| 2556 | return this; |
| 2557 | |
| 2558 | // just one destination. most common case. |
| 2559 | if (state.pipesCount === 1) { |
| 2560 | // passed in one, but it's not the right one. |
| 2561 | if (dest && dest !== state.pipes) |
| 2562 | return this; |
| 2563 | |
| 2564 | if (!dest) |
| 2565 | dest = state.pipes; |
| 2566 | |
| 2567 | // got a match. |
| 2568 | state.pipes = null; |
| 2569 | state.pipesCount = 0; |
| 2570 | this.removeListener('readable', pipeOnReadable); |
| 2571 | state.flowing = false; |
| 2572 | if (dest) |
| 2573 | dest.emit('unpipe', this); |
| 2574 | return this; |
| 2575 | } |
| 2576 | |
| 2577 | // slow case. multiple pipe destinations. |
| 2578 | |
| 2579 | if (!dest) { |
| 2580 | // remove all. |
| 2581 | var dests = state.pipes; |
| 2582 | var len = state.pipesCount; |
| 2583 | state.pipes = null; |
| 2584 | state.pipesCount = 0; |
| 2585 | this.removeListener('readable', pipeOnReadable); |
| 2586 | state.flowing = false; |
| 2587 | |
| 2588 | for (var i = 0; i < len; i++) |
| 2589 | dests[i].emit('unpipe', this); |
| 2590 | return this; |
| 2591 | } |
| 2592 | |
| 2593 | // try to find the right one. |
| 2594 | var i = indexOf(state.pipes, dest); |
| 2595 | if (i === -1) |
| 2596 | return this; |
| 2597 | |
| 2598 | state.pipes.splice(i, 1); |
| 2599 | state.pipesCount -= 1; |
| 2600 | if (state.pipesCount === 1) |
| 2601 | state.pipes = state.pipes[0]; |
| 2602 | |
| 2603 | dest.emit('unpipe', this); |
| 2604 | |
| 2605 | return this; |
| 2606 | }; |
| 2607 | |
| 2608 | // set up data events if they are asked for |
| 2609 | // Ensure readable listeners eventually get something |
| 2610 | Readable.prototype.on = function(ev, fn) { |
| 2611 | var res = Stream.prototype.on.call(this, ev, fn); |
| 2612 | |
| 2613 | if (ev === 'data' && !this._readableState.flowing) |
| 2614 | emitDataEvents(this); |
| 2615 | |
| 2616 | if (ev === 'readable' && this.readable) { |
| 2617 | var state = this._readableState; |
| 2618 | if (!state.readableListening) { |
| 2619 | state.readableListening = true; |
| 2620 | state.emittedReadable = false; |
| 2621 | state.needReadable = true; |
| 2622 | if (!state.reading) { |
| 2623 | this.read(0); |
| 2624 | } else if (state.length) { |
| 2625 | emitReadable(this, state); |
| 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | return res; |
| 2631 | }; |
| 2632 | Readable.prototype.addListener = Readable.prototype.on; |
| 2633 | |
| 2634 | // pause() and resume() are remnants of the legacy readable stream API |
| 2635 | // If the user uses them, then switch into old mode. |
| 2636 | Readable.prototype.resume = function() { |
| 2637 | emitDataEvents(this); |
| 2638 | this.read(0); |
| 2639 | this.emit('resume'); |
| 2640 | }; |
| 2641 | |
| 2642 | Readable.prototype.pause = function() { |
| 2643 | emitDataEvents(this, true); |
| 2644 | this.emit('pause'); |
| 2645 | }; |
| 2646 | |
| 2647 | function emitDataEvents(stream, startPaused) { |
| 2648 | var state = stream._readableState; |
| 2649 | |
| 2650 | if (state.flowing) { |
| 2651 | // https://github.com/isaacs/readable-stream/issues/16 |
| 2652 | throw new Error('Cannot switch to old mode now.'); |
| 2653 | } |
| 2654 | |
| 2655 | var paused = startPaused || false; |
| 2656 | var readable = false; |
| 2657 | |
| 2658 | // convert to an old-style stream. |
| 2659 | stream.readable = true; |
| 2660 | stream.pipe = Stream.prototype.pipe; |
| 2661 | stream.on = stream.addListener = Stream.prototype.on; |
| 2662 | |
| 2663 | stream.on('readable', function() { |
| 2664 | readable = true; |
| 2665 | |
| 2666 | var c; |
| 2667 | while (!paused && (null !== (c = stream.read()))) |
| 2668 | stream.emit('data', c); |
| 2669 | |
| 2670 | if (c === null) { |
| 2671 | readable = false; |
| 2672 | stream._readableState.needReadable = true; |
| 2673 | } |
| 2674 | }); |
| 2675 | |
| 2676 | stream.pause = function() { |
| 2677 | paused = true; |
| 2678 | this.emit('pause'); |
| 2679 | }; |
| 2680 | |
| 2681 | stream.resume = function() { |
| 2682 | paused = false; |
| 2683 | if (readable) |
| 2684 | process.nextTick(function() { |
| 2685 | stream.emit('readable'); |
| 2686 | }); |
| 2687 | else |
| 2688 | this.read(0); |
| 2689 | this.emit('resume'); |
| 2690 | }; |
| 2691 | |
| 2692 | // now make it start, just in case it hadn't already. |
| 2693 | stream.emit('readable'); |
| 2694 | } |
| 2695 | |
| 2696 | // wrap an old-style stream as the async data source. |
| 2697 | // This is *not* part of the readable stream interface. |
| 2698 | // It is an ugly unfortunate mess of history. |
| 2699 | Readable.prototype.wrap = function(stream) { |
| 2700 | var state = this._readableState; |
| 2701 | var paused = false; |
| 2702 | |
| 2703 | var self = this; |
| 2704 | stream.on('end', function() { |
| 2705 | if (state.decoder && !state.ended) { |
| 2706 | var chunk = state.decoder.end(); |
| 2707 | if (chunk && chunk.length) |
| 2708 | self.push(chunk); |
| 2709 | } |
| 2710 | |
| 2711 | self.push(null); |
| 2712 | }); |
| 2713 | |
| 2714 | stream.on('data', function(chunk) { |
| 2715 | if (state.decoder) |
| 2716 | chunk = state.decoder.write(chunk); |
| 2717 | if (!chunk || !state.objectMode && !chunk.length) |
| 2718 | return; |
| 2719 | |
| 2720 | var ret = self.push(chunk); |
| 2721 | if (!ret) { |
| 2722 | paused = true; |
| 2723 | stream.pause(); |
| 2724 | } |
| 2725 | }); |
| 2726 | |
| 2727 | // proxy all the other methods. |
| 2728 | // important when wrapping filters and duplexes. |
| 2729 | for (var i in stream) { |
| 2730 | if (typeof stream[i] === 'function' && |
| 2731 | typeof this[i] === 'undefined') { |
| 2732 | this[i] = function(method) { return function() { |
| 2733 | return stream[method].apply(stream, arguments); |
| 2734 | }}(i); |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | // proxy certain important events. |
| 2739 | var events = ['error', 'close', 'destroy', 'pause', 'resume']; |
| 2740 | forEach(events, function(ev) { |
| 2741 | stream.on(ev, self.emit.bind(self, ev)); |
| 2742 | }); |
| 2743 | |
| 2744 | // when we try to consume some more bytes, simply unpause the |
| 2745 | // underlying stream. |
| 2746 | self._read = function(n) { |
| 2747 | if (paused) { |
| 2748 | paused = false; |
| 2749 | stream.resume(); |
| 2750 | } |
| 2751 | }; |
| 2752 | |
| 2753 | return self; |
| 2754 | }; |
| 2755 | |
| 2756 | |
| 2757 | |
| 2758 | // exposed for testing purposes only. |
| 2759 | Readable._fromList = fromList; |
| 2760 | |
| 2761 | // Pluck off n bytes from an array of buffers. |
| 2762 | // Length is the combined lengths of all the buffers in the list. |
| 2763 | function fromList(n, state) { |
| 2764 | var list = state.buffer; |
| 2765 | var length = state.length; |
| 2766 | var stringMode = !!state.decoder; |
| 2767 | var objectMode = !!state.objectMode; |
| 2768 | var ret; |
| 2769 | |
| 2770 | // nothing in the list, definitely empty. |
| 2771 | if (list.length === 0) |
| 2772 | return null; |
| 2773 | |
| 2774 | if (length === 0) |
| 2775 | ret = null; |
| 2776 | else if (objectMode) |
| 2777 | ret = list.shift(); |
| 2778 | else if (!n || n >= length) { |
| 2779 | // read it all, truncate the array. |
| 2780 | if (stringMode) |
| 2781 | ret = list.join(''); |
| 2782 | else |
| 2783 | ret = Buffer.concat(list, length); |
| 2784 | list.length = 0; |
| 2785 | } else { |
| 2786 | // read just some of it. |
| 2787 | if (n < list[0].length) { |
| 2788 | // just take a part of the first list item. |
| 2789 | // slice is the same for buffers and strings. |
| 2790 | var buf = list[0]; |
| 2791 | ret = buf.slice(0, n); |
| 2792 | list[0] = buf.slice(n); |
| 2793 | } else if (n === list[0].length) { |
| 2794 | // first list is a perfect match |
| 2795 | ret = list.shift(); |
| 2796 | } else { |
| 2797 | // complex case. |
| 2798 | // we have enough to cover it, but it spans past the first buffer. |
| 2799 | if (stringMode) |
| 2800 | ret = ''; |
| 2801 | else |
| 2802 | ret = new Buffer(n); |
| 2803 | |
| 2804 | var c = 0; |
| 2805 | for (var i = 0, l = list.length; i < l && c < n; i++) { |
| 2806 | var buf = list[0]; |
| 2807 | var cpy = Math.min(n - c, buf.length); |
| 2808 | |
| 2809 | if (stringMode) |
| 2810 | ret += buf.slice(0, cpy); |
| 2811 | else |
| 2812 | buf.copy(ret, c, 0, cpy); |
| 2813 | |
| 2814 | if (cpy < buf.length) |
| 2815 | list[0] = buf.slice(cpy); |
| 2816 | else |
| 2817 | list.shift(); |
| 2818 | |
| 2819 | c += cpy; |
| 2820 | } |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | return ret; |
| 2825 | } |
| 2826 | |
| 2827 | function endReadable(stream) { |
| 2828 | var state = stream._readableState; |
| 2829 | |
| 2830 | // If we get here before consuming all the bytes, then that is a |
| 2831 | // bug in node. Should never happen. |
| 2832 | if (state.length > 0) |
| 2833 | throw new Error('endReadable called on non-empty stream'); |
| 2834 | |
| 2835 | if (!state.endEmitted && state.calledRead) { |
| 2836 | state.ended = true; |
| 2837 | process.nextTick(function() { |
| 2838 | // Check that we didn't get one last unshift. |
| 2839 | if (!state.endEmitted && state.length === 0) { |
| 2840 | state.endEmitted = true; |
| 2841 | stream.readable = false; |
| 2842 | stream.emit('end'); |
| 2843 | } |
| 2844 | }); |
| 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | function forEach (xs, f) { |
| 2849 | for (var i = 0, l = xs.length; i < l; i++) { |
| 2850 | f(xs[i], i); |
| 2851 | } |
| 2852 | } |
| 2853 | |
| 2854 | function indexOf (xs, x) { |
| 2855 | for (var i = 0, l = xs.length; i < l; i++) { |
| 2856 | if (xs[i] === x) return i; |
| 2857 | } |
| 2858 | return -1; |
| 2859 | } |
| 2860 | |
| 2861 | }).call(this,require("4dON6Z")) |
| 2862 | },{"4dON6Z":6,"buffer":1,"core-util-is":13,"events":4,"inherits":5,"isarray":14,"stream":20,"string_decoder/":15}],11:[function(require,module,exports){ |
| 2863 | // Copyright Joyent, Inc. and other Node contributors. |
| 2864 | // |
| 2865 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 2866 | // copy of this software and associated documentation files (the |
| 2867 | // "Software"), to deal in the Software without restriction, including |
| 2868 | // without limitation the rights to use, copy, modify, merge, publish, |
| 2869 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 2870 | // persons to whom the Software is furnished to do so, subject to the |
| 2871 | // following conditions: |
| 2872 | // |
| 2873 | // The above copyright notice and this permission notice shall be included |
| 2874 | // in all copies or substantial portions of the Software. |
| 2875 | // |
| 2876 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 2877 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 2878 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 2879 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 2880 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 2881 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 2882 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 2883 | |
| 2884 | |
| 2885 | // a transform stream is a readable/writable stream where you do |
| 2886 | // something with the data. Sometimes it's called a "filter", |
| 2887 | // but that's not a great name for it, since that implies a thing where |
| 2888 | // some bits pass through, and others are simply ignored. (That would |
| 2889 | // be a valid example of a transform, of course.) |
| 2890 | // |
| 2891 | // While the output is causally related to the input, it's not a |
| 2892 | // necessarily symmetric or synchronous transformation. For example, |
| 2893 | // a zlib stream might take multiple plain-text writes(), and then |
| 2894 | // emit a single compressed chunk some time in the future. |
| 2895 | // |
| 2896 | // Here's how this works: |
| 2897 | // |
| 2898 | // The Transform stream has all the aspects of the readable and writable |
| 2899 | // stream classes. When you write(chunk), that calls _write(chunk,cb) |
| 2900 | // internally, and returns false if there's a lot of pending writes |
| 2901 | // buffered up. When you call read(), that calls _read(n) until |
| 2902 | // there's enough pending readable data buffered up. |
| 2903 | // |
| 2904 | // In a transform stream, the written data is placed in a buffer. When |
| 2905 | // _read(n) is called, it transforms the queued up data, calling the |
| 2906 | // buffered _write cb's as it consumes chunks. If consuming a single |
| 2907 | // written chunk would result in multiple output chunks, then the first |
| 2908 | // outputted bit calls the readcb, and subsequent chunks just go into |
| 2909 | // the read buffer, and will cause it to emit 'readable' if necessary. |
| 2910 | // |
| 2911 | // This way, back-pressure is actually determined by the reading side, |
| 2912 | // since _read has to be called to start processing a new chunk. However, |
| 2913 | // a pathological inflate type of transform can cause excessive buffering |
| 2914 | // here. For example, imagine a stream where every byte of input is |
| 2915 | // interpreted as an integer from 0-255, and then results in that many |
| 2916 | // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in |
| 2917 | // 1kb of data being output. In this case, you could write a very small |
| 2918 | // amount of input, and end up with a very large amount of output. In |
| 2919 | // such a pathological inflating mechanism, there'd be no way to tell |
| 2920 | // the system to stop doing the transform. A single 4MB write could |
| 2921 | // cause the system to run out of memory. |
| 2922 | // |
| 2923 | // However, even in such a pathological case, only a single written chunk |
| 2924 | // would be consumed, and then the rest would wait (un-transformed) until |
| 2925 | // the results of the previous transformed chunk were consumed. |
| 2926 | |
| 2927 | module.exports = Transform; |
| 2928 | |
| 2929 | var Duplex = require('./_stream_duplex'); |
| 2930 | |
| 2931 | /*<replacement>*/ |
| 2932 | var util = require('core-util-is'); |
| 2933 | util.inherits = require('inherits'); |
| 2934 | /*</replacement>*/ |
| 2935 | |
| 2936 | util.inherits(Transform, Duplex); |
| 2937 | |
| 2938 | |
| 2939 | function TransformState(options, stream) { |
| 2940 | this.afterTransform = function(er, data) { |
| 2941 | return afterTransform(stream, er, data); |
| 2942 | }; |
| 2943 | |
| 2944 | this.needTransform = false; |
| 2945 | this.transforming = false; |
| 2946 | this.writecb = null; |
| 2947 | this.writechunk = null; |
| 2948 | } |
| 2949 | |
| 2950 | function afterTransform(stream, er, data) { |
| 2951 | var ts = stream._transformState; |
| 2952 | ts.transforming = false; |
| 2953 | |
| 2954 | var cb = ts.writecb; |
| 2955 | |
| 2956 | if (!cb) |
| 2957 | return stream.emit('error', new Error('no writecb in Transform class')); |
| 2958 | |
| 2959 | ts.writechunk = null; |
| 2960 | ts.writecb = null; |
| 2961 | |
| 2962 | if (data !== null && data !== undefined) |
| 2963 | stream.push(data); |
| 2964 | |
| 2965 | if (cb) |
| 2966 | cb(er); |
| 2967 | |
| 2968 | var rs = stream._readableState; |
| 2969 | rs.reading = false; |
| 2970 | if (rs.needReadable || rs.length < rs.highWaterMark) { |
| 2971 | stream._read(rs.highWaterMark); |
| 2972 | } |
| 2973 | } |
| 2974 | |
| 2975 | |
| 2976 | function Transform(options) { |
| 2977 | if (!(this instanceof Transform)) |
| 2978 | return new Transform(options); |
| 2979 | |
| 2980 | Duplex.call(this, options); |
| 2981 | |
| 2982 | var ts = this._transformState = new TransformState(options, this); |
| 2983 | |
| 2984 | // when the writable side finishes, then flush out anything remaining. |
| 2985 | var stream = this; |
| 2986 | |
| 2987 | // start out asking for a readable event once data is transformed. |
| 2988 | this._readableState.needReadable = true; |
| 2989 | |
| 2990 | // we have implemented the _read method, and done the other things |
| 2991 | // that Readable wants before the first _read call, so unset the |
| 2992 | // sync guard flag. |
| 2993 | this._readableState.sync = false; |
| 2994 | |
| 2995 | this.once('finish', function() { |
| 2996 | if ('function' === typeof this._flush) |
| 2997 | this._flush(function(er) { |
| 2998 | done(stream, er); |
| 2999 | }); |
| 3000 | else |
| 3001 | done(stream); |
| 3002 | }); |
| 3003 | } |
| 3004 | |
| 3005 | Transform.prototype.push = function(chunk, encoding) { |
| 3006 | this._transformState.needTransform = false; |
| 3007 | return Duplex.prototype.push.call(this, chunk, encoding); |
| 3008 | }; |
| 3009 | |
| 3010 | // This is the part where you do stuff! |
| 3011 | // override this function in implementation classes. |
| 3012 | // 'chunk' is an input chunk. |
| 3013 | // |
| 3014 | // Call `push(newChunk)` to pass along transformed output |
| 3015 | // to the readable side. You may call 'push' zero or more times. |
| 3016 | // |
| 3017 | // Call `cb(err)` when you are done with this chunk. If you pass |
| 3018 | // an error, then that'll put the hurt on the whole operation. If you |
| 3019 | // never call cb(), then you'll never get another chunk. |
| 3020 | Transform.prototype._transform = function(chunk, encoding, cb) { |
| 3021 | throw new Error('not implemented'); |
| 3022 | }; |
| 3023 | |
| 3024 | Transform.prototype._write = function(chunk, encoding, cb) { |
| 3025 | var ts = this._transformState; |
| 3026 | ts.writecb = cb; |
| 3027 | ts.writechunk = chunk; |
| 3028 | ts.writeencoding = encoding; |
| 3029 | if (!ts.transforming) { |
| 3030 | var rs = this._readableState; |
| 3031 | if (ts.needTransform || |
| 3032 | rs.needReadable || |
| 3033 | rs.length < rs.highWaterMark) |
| 3034 | this._read(rs.highWaterMark); |
| 3035 | } |
| 3036 | }; |
| 3037 | |
| 3038 | // Doesn't matter what the args are here. |
| 3039 | // _transform does all the work. |
| 3040 | // That we got here means that the readable side wants more data. |
| 3041 | Transform.prototype._read = function(n) { |
| 3042 | var ts = this._transformState; |
| 3043 | |
| 3044 | if (ts.writechunk !== null && ts.writecb && !ts.transforming) { |
| 3045 | ts.transforming = true; |
| 3046 | this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); |
| 3047 | } else { |
| 3048 | // mark that we need a transform, so that any data that comes in |
| 3049 | // will get processed, now that we've asked for it. |
| 3050 | ts.needTransform = true; |
| 3051 | } |
| 3052 | }; |
| 3053 | |
| 3054 | |
| 3055 | function done(stream, er) { |
| 3056 | if (er) |
| 3057 | return stream.emit('error', er); |
| 3058 | |
| 3059 | // if there's nothing in the write buffer, then that means |
| 3060 | // that nothing more will ever be provided |
| 3061 | var ws = stream._writableState; |
| 3062 | var rs = stream._readableState; |
| 3063 | var ts = stream._transformState; |
| 3064 | |
| 3065 | if (ws.length) |
| 3066 | throw new Error('calling transform done when ws.length != 0'); |
| 3067 | |
| 3068 | if (ts.transforming) |
| 3069 | throw new Error('calling transform done when still transforming'); |
| 3070 | |
| 3071 | return stream.push(null); |
| 3072 | } |
| 3073 | |
| 3074 | },{"./_stream_duplex":8,"core-util-is":13,"inherits":5}],12:[function(require,module,exports){ |
| 3075 | (function (process){ |
| 3076 | // Copyright Joyent, Inc. and other Node contributors. |
| 3077 | // |
| 3078 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 3079 | // copy of this software and associated documentation files (the |
| 3080 | // "Software"), to deal in the Software without restriction, including |
| 3081 | // without limitation the rights to use, copy, modify, merge, publish, |
| 3082 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 3083 | // persons to whom the Software is furnished to do so, subject to the |
| 3084 | // following conditions: |
| 3085 | // |
| 3086 | // The above copyright notice and this permission notice shall be included |
| 3087 | // in all copies or substantial portions of the Software. |
| 3088 | // |
| 3089 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 3090 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 3091 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 3092 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 3093 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 3094 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 3095 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 3096 | |
| 3097 | // A bit simpler than readable streams. |
| 3098 | // Implement an async ._write(chunk, cb), and it'll handle all |
| 3099 | // the drain event emission and buffering. |
| 3100 | |
| 3101 | module.exports = Writable; |
| 3102 | |
| 3103 | /*<replacement>*/ |
| 3104 | var Buffer = require('buffer').Buffer; |
| 3105 | /*</replacement>*/ |
| 3106 | |
| 3107 | Writable.WritableState = WritableState; |
| 3108 | |
| 3109 | |
| 3110 | /*<replacement>*/ |
| 3111 | var util = require('core-util-is'); |
| 3112 | util.inherits = require('inherits'); |
| 3113 | /*</replacement>*/ |
| 3114 | |
| 3115 | |
| 3116 | var Stream = require('stream'); |
| 3117 | |
| 3118 | util.inherits(Writable, Stream); |
| 3119 | |
| 3120 | function WriteReq(chunk, encoding, cb) { |
| 3121 | this.chunk = chunk; |
| 3122 | this.encoding = encoding; |
| 3123 | this.callback = cb; |
| 3124 | } |
| 3125 | |
| 3126 | function WritableState(options, stream) { |
| 3127 | options = options || {}; |
| 3128 | |
| 3129 | // the point at which write() starts returning false |
| 3130 | // Note: 0 is a valid value, means that we always return false if |
| 3131 | // the entire buffer is not flushed immediately on write() |
| 3132 | var hwm = options.highWaterMark; |
| 3133 | this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024; |
| 3134 | |
| 3135 | // object stream flag to indicate whether or not this stream |
| 3136 | // contains buffers or objects. |
| 3137 | this.objectMode = !!options.objectMode; |
| 3138 | |
| 3139 | // cast to ints. |
| 3140 | this.highWaterMark = ~~this.highWaterMark; |
| 3141 | |
| 3142 | this.needDrain = false; |
| 3143 | // at the start of calling end() |
| 3144 | this.ending = false; |
| 3145 | // when end() has been called, and returned |
| 3146 | this.ended = false; |
| 3147 | // when 'finish' is emitted |
| 3148 | this.finished = false; |
| 3149 | |
| 3150 | // should we decode strings into buffers before passing to _write? |
| 3151 | // this is here so that some node-core streams can optimize string |
| 3152 | // handling at a lower level. |
| 3153 | var noDecode = options.decodeStrings === false; |
| 3154 | this.decodeStrings = !noDecode; |
| 3155 | |
| 3156 | // Crypto is kind of old and crusty. Historically, its default string |
| 3157 | // encoding is 'binary' so we have to make this configurable. |
| 3158 | // Everything else in the universe uses 'utf8', though. |
| 3159 | this.defaultEncoding = options.defaultEncoding || 'utf8'; |
| 3160 | |
| 3161 | // not an actual buffer we keep track of, but a measurement |
| 3162 | // of how much we're waiting to get pushed to some underlying |
| 3163 | // socket or file. |
| 3164 | this.length = 0; |
| 3165 | |
| 3166 | // a flag to see when we're in the middle of a write. |
| 3167 | this.writing = false; |
| 3168 | |
| 3169 | // a flag to be able to tell if the onwrite cb is called immediately, |
| 3170 | // or on a later tick. We set this to true at first, becuase any |
| 3171 | // actions that shouldn't happen until "later" should generally also |
| 3172 | // not happen before the first write call. |
| 3173 | this.sync = true; |
| 3174 | |
| 3175 | // a flag to know if we're processing previously buffered items, which |
| 3176 | // may call the _write() callback in the same tick, so that we don't |
| 3177 | // end up in an overlapped onwrite situation. |
| 3178 | this.bufferProcessing = false; |
| 3179 | |
| 3180 | // the callback that's passed to _write(chunk,cb) |
| 3181 | this.onwrite = function(er) { |
| 3182 | onwrite(stream, er); |
| 3183 | }; |
| 3184 | |
| 3185 | // the callback that the user supplies to write(chunk,encoding,cb) |
| 3186 | this.writecb = null; |
| 3187 | |
| 3188 | // the amount that is being written when _write is called. |
| 3189 | this.writelen = 0; |
| 3190 | |
| 3191 | this.buffer = []; |
| 3192 | |
| 3193 | // True if the error was already emitted and should not be thrown again |
| 3194 | this.errorEmitted = false; |
| 3195 | } |
| 3196 | |
| 3197 | function Writable(options) { |
| 3198 | var Duplex = require('./_stream_duplex'); |
| 3199 | |
| 3200 | // Writable ctor is applied to Duplexes, though they're not |
| 3201 | // instanceof Writable, they're instanceof Readable. |
| 3202 | if (!(this instanceof Writable) && !(this instanceof Duplex)) |
| 3203 | return new Writable(options); |
| 3204 | |
| 3205 | this._writableState = new WritableState(options, this); |
| 3206 | |
| 3207 | // legacy. |
| 3208 | this.writable = true; |
| 3209 | |
| 3210 | Stream.call(this); |
| 3211 | } |
| 3212 | |
| 3213 | // Otherwise people can pipe Writable streams, which is just wrong. |
| 3214 | Writable.prototype.pipe = function() { |
| 3215 | this.emit('error', new Error('Cannot pipe. Not readable.')); |
| 3216 | }; |
| 3217 | |
| 3218 | |
| 3219 | function writeAfterEnd(stream, state, cb) { |
| 3220 | var er = new Error('write after end'); |
| 3221 | // TODO: defer error events consistently everywhere, not just the cb |
| 3222 | stream.emit('error', er); |
| 3223 | process.nextTick(function() { |
| 3224 | cb(er); |
| 3225 | }); |
| 3226 | } |
| 3227 | |
| 3228 | // If we get something that is not a buffer, string, null, or undefined, |
| 3229 | // and we're not in objectMode, then that's an error. |
| 3230 | // Otherwise stream chunks are all considered to be of length=1, and the |
| 3231 | // watermarks determine how many objects to keep in the buffer, rather than |
| 3232 | // how many bytes or characters. |
| 3233 | function validChunk(stream, state, chunk, cb) { |
| 3234 | var valid = true; |
| 3235 | if (!Buffer.isBuffer(chunk) && |
| 3236 | 'string' !== typeof chunk && |
| 3237 | chunk !== null && |
| 3238 | chunk !== undefined && |
| 3239 | !state.objectMode) { |
| 3240 | var er = new TypeError('Invalid non-string/buffer chunk'); |
| 3241 | stream.emit('error', er); |
| 3242 | process.nextTick(function() { |
| 3243 | cb(er); |
| 3244 | }); |
| 3245 | valid = false; |
| 3246 | } |
| 3247 | return valid; |
| 3248 | } |
| 3249 | |
| 3250 | Writable.prototype.write = function(chunk, encoding, cb) { |
| 3251 | var state = this._writableState; |
| 3252 | var ret = false; |
| 3253 | |
| 3254 | if (typeof encoding === 'function') { |
| 3255 | cb = encoding; |
| 3256 | encoding = null; |
| 3257 | } |
| 3258 | |
| 3259 | if (Buffer.isBuffer(chunk)) |
| 3260 | encoding = 'buffer'; |
| 3261 | else if (!encoding) |
| 3262 | encoding = state.defaultEncoding; |
| 3263 | |
| 3264 | if (typeof cb !== 'function') |
| 3265 | cb = function() {}; |
| 3266 | |
| 3267 | if (state.ended) |
| 3268 | writeAfterEnd(this, state, cb); |
| 3269 | else if (validChunk(this, state, chunk, cb)) |
| 3270 | ret = writeOrBuffer(this, state, chunk, encoding, cb); |
| 3271 | |
| 3272 | return ret; |
| 3273 | }; |
| 3274 | |
| 3275 | function decodeChunk(state, chunk, encoding) { |
| 3276 | if (!state.objectMode && |
| 3277 | state.decodeStrings !== false && |
| 3278 | typeof chunk === 'string') { |
| 3279 | chunk = new Buffer(chunk, encoding); |
| 3280 | } |
| 3281 | return chunk; |
| 3282 | } |
| 3283 | |
| 3284 | // if we're already writing something, then just put this |
| 3285 | // in the queue, and wait our turn. Otherwise, call _write |
| 3286 | // If we return false, then we need a drain event, so set that flag. |
| 3287 | function writeOrBuffer(stream, state, chunk, encoding, cb) { |
| 3288 | chunk = decodeChunk(state, chunk, encoding); |
| 3289 | if (Buffer.isBuffer(chunk)) |
| 3290 | encoding = 'buffer'; |
| 3291 | var len = state.objectMode ? 1 : chunk.length; |
| 3292 | |
| 3293 | state.length += len; |
| 3294 | |
| 3295 | var ret = state.length < state.highWaterMark; |
| 3296 | // we must ensure that previous needDrain will not be reset to false. |
| 3297 | if (!ret) |
| 3298 | state.needDrain = true; |
| 3299 | |
| 3300 | if (state.writing) |
| 3301 | state.buffer.push(new WriteReq(chunk, encoding, cb)); |
| 3302 | else |
| 3303 | doWrite(stream, state, len, chunk, encoding, cb); |
| 3304 | |
| 3305 | return ret; |
| 3306 | } |
| 3307 | |
| 3308 | function doWrite(stream, state, len, chunk, encoding, cb) { |
| 3309 | state.writelen = len; |
| 3310 | state.writecb = cb; |
| 3311 | state.writing = true; |
| 3312 | state.sync = true; |
| 3313 | stream._write(chunk, encoding, state.onwrite); |
| 3314 | state.sync = false; |
| 3315 | } |
| 3316 | |
| 3317 | function onwriteError(stream, state, sync, er, cb) { |
| 3318 | if (sync) |
| 3319 | process.nextTick(function() { |
| 3320 | cb(er); |
| 3321 | }); |
| 3322 | else |
| 3323 | cb(er); |
| 3324 | |
| 3325 | stream._writableState.errorEmitted = true; |
| 3326 | stream.emit('error', er); |
| 3327 | } |
| 3328 | |
| 3329 | function onwriteStateUpdate(state) { |
| 3330 | state.writing = false; |
| 3331 | state.writecb = null; |
| 3332 | state.length -= state.writelen; |
| 3333 | state.writelen = 0; |
| 3334 | } |
| 3335 | |
| 3336 | function onwrite(stream, er) { |
| 3337 | var state = stream._writableState; |
| 3338 | var sync = state.sync; |
| 3339 | var cb = state.writecb; |
| 3340 | |
| 3341 | onwriteStateUpdate(state); |
| 3342 | |
| 3343 | if (er) |
| 3344 | onwriteError(stream, state, sync, er, cb); |
| 3345 | else { |
| 3346 | // Check if we're actually ready to finish, but don't emit yet |
| 3347 | var finished = needFinish(stream, state); |
| 3348 | |
| 3349 | if (!finished && !state.bufferProcessing && state.buffer.length) |
| 3350 | clearBuffer(stream, state); |
| 3351 | |
| 3352 | if (sync) { |
| 3353 | process.nextTick(function() { |
| 3354 | afterWrite(stream, state, finished, cb); |
| 3355 | }); |
| 3356 | } else { |
| 3357 | afterWrite(stream, state, finished, cb); |
| 3358 | } |
| 3359 | } |
| 3360 | } |
| 3361 | |
| 3362 | function afterWrite(stream, state, finished, cb) { |
| 3363 | if (!finished) |
| 3364 | onwriteDrain(stream, state); |
| 3365 | cb(); |
| 3366 | if (finished) |
| 3367 | finishMaybe(stream, state); |
| 3368 | } |
| 3369 | |
| 3370 | // Must force callback to be called on nextTick, so that we don't |
| 3371 | // emit 'drain' before the write() consumer gets the 'false' return |
| 3372 | // value, and has a chance to attach a 'drain' listener. |
| 3373 | function onwriteDrain(stream, state) { |
| 3374 | if (state.length === 0 && state.needDrain) { |
| 3375 | state.needDrain = false; |
| 3376 | stream.emit('drain'); |
| 3377 | } |
| 3378 | } |
| 3379 | |
| 3380 | |
| 3381 | // if there's something in the buffer waiting, then process it |
| 3382 | function clearBuffer(stream, state) { |
| 3383 | state.bufferProcessing = true; |
| 3384 | |
| 3385 | for (var c = 0; c < state.buffer.length; c++) { |
| 3386 | var entry = state.buffer[c]; |
| 3387 | var chunk = entry.chunk; |
| 3388 | var encoding = entry.encoding; |
| 3389 | var cb = entry.callback; |
| 3390 | var len = state.objectMode ? 1 : chunk.length; |
| 3391 | |
| 3392 | doWrite(stream, state, len, chunk, encoding, cb); |
| 3393 | |
| 3394 | // if we didn't call the onwrite immediately, then |
| 3395 | // it means that we need to wait until it does. |
| 3396 | // also, that means that the chunk and cb are currently |
| 3397 | // being processed, so move the buffer counter past them. |
| 3398 | if (state.writing) { |
| 3399 | c++; |
| 3400 | break; |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | state.bufferProcessing = false; |
| 3405 | if (c < state.buffer.length) |
| 3406 | state.buffer = state.buffer.slice(c); |
| 3407 | else |
| 3408 | state.buffer.length = 0; |
| 3409 | } |
| 3410 | |
| 3411 | Writable.prototype._write = function(chunk, encoding, cb) { |
| 3412 | cb(new Error('not implemented')); |
| 3413 | }; |
| 3414 | |
| 3415 | Writable.prototype.end = function(chunk, encoding, cb) { |
| 3416 | var state = this._writableState; |
| 3417 | |
| 3418 | if (typeof chunk === 'function') { |
| 3419 | cb = chunk; |
| 3420 | chunk = null; |
| 3421 | encoding = null; |
| 3422 | } else if (typeof encoding === 'function') { |
| 3423 | cb = encoding; |
| 3424 | encoding = null; |
| 3425 | } |
| 3426 | |
| 3427 | if (typeof chunk !== 'undefined' && chunk !== null) |
| 3428 | this.write(chunk, encoding); |
| 3429 | |
| 3430 | // ignore unnecessary end() calls. |
| 3431 | if (!state.ending && !state.finished) |
| 3432 | endWritable(this, state, cb); |
| 3433 | }; |
| 3434 | |
| 3435 | |
| 3436 | function needFinish(stream, state) { |
| 3437 | return (state.ending && |
| 3438 | state.length === 0 && |
| 3439 | !state.finished && |
| 3440 | !state.writing); |
| 3441 | } |
| 3442 | |
| 3443 | function finishMaybe(stream, state) { |
| 3444 | var need = needFinish(stream, state); |
| 3445 | if (need) { |
| 3446 | state.finished = true; |
| 3447 | stream.emit('finish'); |
| 3448 | } |
| 3449 | return need; |
| 3450 | } |
| 3451 | |
| 3452 | function endWritable(stream, state, cb) { |
| 3453 | state.ending = true; |
| 3454 | finishMaybe(stream, state); |
| 3455 | if (cb) { |
| 3456 | if (state.finished) |
| 3457 | process.nextTick(cb); |
| 3458 | else |
| 3459 | stream.once('finish', cb); |
| 3460 | } |
| 3461 | state.ended = true; |
| 3462 | } |
| 3463 | |
| 3464 | }).call(this,require("4dON6Z")) |
| 3465 | },{"./_stream_duplex":8,"4dON6Z":6,"buffer":1,"core-util-is":13,"inherits":5,"stream":20}],13:[function(require,module,exports){ |
| 3466 | (function (Buffer){ |
| 3467 | // Copyright Joyent, Inc. and other Node contributors. |
| 3468 | // |
| 3469 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 3470 | // copy of this software and associated documentation files (the |
| 3471 | // "Software"), to deal in the Software without restriction, including |
| 3472 | // without limitation the rights to use, copy, modify, merge, publish, |
| 3473 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 3474 | // persons to whom the Software is furnished to do so, subject to the |
| 3475 | // following conditions: |
| 3476 | // |
| 3477 | // The above copyright notice and this permission notice shall be included |
| 3478 | // in all copies or substantial portions of the Software. |
| 3479 | // |
| 3480 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 3481 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 3482 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 3483 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 3484 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 3485 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 3486 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 3487 | |
| 3488 | // NOTE: These type checking functions intentionally don't use `instanceof` |
| 3489 | // because it is fragile and can be easily faked with `Object.create()`. |
| 3490 | function isArray(ar) { |
| 3491 | return Array.isArray(ar); |
| 3492 | } |
| 3493 | exports.isArray = isArray; |
| 3494 | |
| 3495 | function isBoolean(arg) { |
| 3496 | return typeof arg === 'boolean'; |
| 3497 | } |
| 3498 | exports.isBoolean = isBoolean; |
| 3499 | |
| 3500 | function isNull(arg) { |
| 3501 | return arg === null; |
| 3502 | } |
| 3503 | exports.isNull = isNull; |
| 3504 | |
| 3505 | function isNullOrUndefined(arg) { |
| 3506 | return arg == null; |
| 3507 | } |
| 3508 | exports.isNullOrUndefined = isNullOrUndefined; |
| 3509 | |
| 3510 | function isNumber(arg) { |
| 3511 | return typeof arg === 'number'; |
| 3512 | } |
| 3513 | exports.isNumber = isNumber; |
| 3514 | |
| 3515 | function isString(arg) { |
| 3516 | return typeof arg === 'string'; |
| 3517 | } |
| 3518 | exports.isString = isString; |
| 3519 | |
| 3520 | function isSymbol(arg) { |
| 3521 | return typeof arg === 'symbol'; |
| 3522 | } |
| 3523 | exports.isSymbol = isSymbol; |
| 3524 | |
| 3525 | function isUndefined(arg) { |
| 3526 | return arg === void 0; |
| 3527 | } |
| 3528 | exports.isUndefined = isUndefined; |
| 3529 | |
| 3530 | function isRegExp(re) { |
| 3531 | return isObject(re) && objectToString(re) === '[object RegExp]'; |
| 3532 | } |
| 3533 | exports.isRegExp = isRegExp; |
| 3534 | |
| 3535 | function isObject(arg) { |
| 3536 | return typeof arg === 'object' && arg !== null; |
| 3537 | } |
| 3538 | exports.isObject = isObject; |
| 3539 | |
| 3540 | function isDate(d) { |
| 3541 | return isObject(d) && objectToString(d) === '[object Date]'; |
| 3542 | } |
| 3543 | exports.isDate = isDate; |
| 3544 | |
| 3545 | function isError(e) { |
| 3546 | return isObject(e) && |
| 3547 | (objectToString(e) === '[object Error]' || e instanceof Error); |
| 3548 | } |
| 3549 | exports.isError = isError; |
| 3550 | |
| 3551 | function isFunction(arg) { |
| 3552 | return typeof arg === 'function'; |
| 3553 | } |
| 3554 | exports.isFunction = isFunction; |
| 3555 | |
| 3556 | function isPrimitive(arg) { |
| 3557 | return arg === null || |
| 3558 | typeof arg === 'boolean' || |
| 3559 | typeof arg === 'number' || |
| 3560 | typeof arg === 'string' || |
| 3561 | typeof arg === 'symbol' || // ES6 symbol |
| 3562 | typeof arg === 'undefined'; |
| 3563 | } |
| 3564 | exports.isPrimitive = isPrimitive; |
| 3565 | |
| 3566 | function isBuffer(arg) { |
| 3567 | return Buffer.isBuffer(arg); |
| 3568 | } |
| 3569 | exports.isBuffer = isBuffer; |
| 3570 | |
| 3571 | function objectToString(o) { |
| 3572 | return Object.prototype.toString.call(o); |
| 3573 | } |
| 3574 | }).call(this,require("buffer").Buffer) |
| 3575 | },{"buffer":1}],14:[function(require,module,exports){ |
| 3576 | module.exports = Array.isArray || function (arr) { |
| 3577 | return Object.prototype.toString.call(arr) == '[object Array]'; |
| 3578 | }; |
| 3579 | |
| 3580 | },{}],15:[function(require,module,exports){ |
| 3581 | // Copyright Joyent, Inc. and other Node contributors. |
| 3582 | // |
| 3583 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 3584 | // copy of this software and associated documentation files (the |
| 3585 | // "Software"), to deal in the Software without restriction, including |
| 3586 | // without limitation the rights to use, copy, modify, merge, publish, |
| 3587 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 3588 | // persons to whom the Software is furnished to do so, subject to the |
| 3589 | // following conditions: |
| 3590 | // |
| 3591 | // The above copyright notice and this permission notice shall be included |
| 3592 | // in all copies or substantial portions of the Software. |
| 3593 | // |
| 3594 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 3595 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 3596 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 3597 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 3598 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 3599 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 3600 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 3601 | |
| 3602 | var Buffer = require('buffer').Buffer; |
| 3603 | |
| 3604 | var isBufferEncoding = Buffer.isEncoding |
| 3605 | || function(encoding) { |
| 3606 | switch (encoding && encoding.toLowerCase()) { |
| 3607 | 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; |
| 3608 | default: return false; |
| 3609 | } |
| 3610 | } |
| 3611 | |
| 3612 | |
| 3613 | function assertEncoding(encoding) { |
| 3614 | if (encoding && !isBufferEncoding(encoding)) { |
| 3615 | throw new Error('Unknown encoding: ' + encoding); |
| 3616 | } |
| 3617 | } |
| 3618 | |
| 3619 | var StringDecoder = exports.StringDecoder = function(encoding) { |
| 3620 | this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, ''); |
| 3621 | assertEncoding(encoding); |
| 3622 | switch (this.encoding) { |
| 3623 | case 'utf8': |
| 3624 | // CESU-8 represents each of Surrogate Pair by 3-bytes |
| 3625 | this.surrogateSize = 3; |
| 3626 | break; |
| 3627 | case 'ucs2': |
| 3628 | case 'utf16le': |
| 3629 | // UTF-16 represents each of Surrogate Pair by 2-bytes |
| 3630 | this.surrogateSize = 2; |
| 3631 | this.detectIncompleteChar = utf16DetectIncompleteChar; |
| 3632 | break; |
| 3633 | case 'base64': |
| 3634 | // Base-64 stores 3 bytes in 4 chars, and pads the remainder. |
| 3635 | this.surrogateSize = 3; |
| 3636 | this.detectIncompleteChar = base64DetectIncompleteChar; |
| 3637 | break; |
| 3638 | default: |
| 3639 | this.write = passThroughWrite; |
| 3640 | return; |
| 3641 | } |
| 3642 | |
| 3643 | this.charBuffer = new Buffer(6); |
| 3644 | this.charReceived = 0; |
| 3645 | this.charLength = 0; |
| 3646 | }; |
| 3647 | |
| 3648 | |
| 3649 | StringDecoder.prototype.write = function(buffer) { |
| 3650 | var charStr = ''; |
| 3651 | var offset = 0; |
| 3652 | |
| 3653 | // if our last write ended with an incomplete multibyte character |
| 3654 | while (this.charLength) { |
| 3655 | // determine how many remaining bytes this buffer has to offer for this char |
| 3656 | var i = (buffer.length >= this.charLength - this.charReceived) ? |
| 3657 | this.charLength - this.charReceived : |
| 3658 | buffer.length; |
| 3659 | |
| 3660 | // add the new bytes to the char buffer |
| 3661 | buffer.copy(this.charBuffer, this.charReceived, offset, i); |
| 3662 | this.charReceived += (i - offset); |
| 3663 | offset = i; |
| 3664 | |
| 3665 | if (this.charReceived < this.charLength) { |
| 3666 | // still not enough chars in this buffer? wait for more ... |
| 3667 | return ''; |
| 3668 | } |
| 3669 | |
| 3670 | // get the character that was split |
| 3671 | charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding); |
| 3672 | |
| 3673 | // lead surrogate (D800-DBFF) is also the incomplete character |
| 3674 | var charCode = charStr.charCodeAt(charStr.length - 1); |
| 3675 | if (charCode >= 0xD800 && charCode <= 0xDBFF) { |
| 3676 | this.charLength += this.surrogateSize; |
| 3677 | charStr = ''; |
| 3678 | continue; |
| 3679 | } |
| 3680 | this.charReceived = this.charLength = 0; |
| 3681 | |
| 3682 | // if there are no more bytes in this buffer, just emit our char |
| 3683 | if (i == buffer.length) return charStr; |
| 3684 | |
| 3685 | // otherwise cut off the characters end from the beginning of this buffer |
| 3686 | buffer = buffer.slice(i, buffer.length); |
| 3687 | break; |
| 3688 | } |
| 3689 | |
| 3690 | var lenIncomplete = this.detectIncompleteChar(buffer); |
| 3691 | |
| 3692 | var end = buffer.length; |
| 3693 | if (this.charLength) { |
| 3694 | // buffer the incomplete character bytes we got |
| 3695 | buffer.copy(this.charBuffer, 0, buffer.length - lenIncomplete, end); |
| 3696 | this.charReceived = lenIncomplete; |
| 3697 | end -= lenIncomplete; |
| 3698 | } |
| 3699 | |
| 3700 | charStr += buffer.toString(this.encoding, 0, end); |
| 3701 | |
| 3702 | var end = charStr.length - 1; |
| 3703 | var charCode = charStr.charCodeAt(end); |
| 3704 | // lead surrogate (D800-DBFF) is also the incomplete character |
| 3705 | if (charCode >= 0xD800 && charCode <= 0xDBFF) { |
| 3706 | var size = this.surrogateSize; |
| 3707 | this.charLength += size; |
| 3708 | this.charReceived += size; |
| 3709 | this.charBuffer.copy(this.charBuffer, size, 0, size); |
| 3710 | this.charBuffer.write(charStr.charAt(charStr.length - 1), this.encoding); |
| 3711 | return charStr.substring(0, end); |
| 3712 | } |
| 3713 | |
| 3714 | // or just emit the charStr |
| 3715 | return charStr; |
| 3716 | }; |
| 3717 | |
| 3718 | StringDecoder.prototype.detectIncompleteChar = function(buffer) { |
| 3719 | // determine how many bytes we have to check at the end of this buffer |
| 3720 | var i = (buffer.length >= 3) ? 3 : buffer.length; |
| 3721 | |
| 3722 | // Figure out if one of the last i bytes of our buffer announces an |
| 3723 | // incomplete char. |
| 3724 | for (; i > 0; i--) { |
| 3725 | var c = buffer[buffer.length - i]; |
| 3726 | |
| 3727 | // See http://en.wikipedia.org/wiki/UTF-8#Description |
| 3728 | |
| 3729 | // 110XXXXX |
| 3730 | if (i == 1 && c >> 5 == 0x06) { |
| 3731 | this.charLength = 2; |
| 3732 | break; |
| 3733 | } |
| 3734 | |
| 3735 | // 1110XXXX |
| 3736 | if (i <= 2 && c >> 4 == 0x0E) { |
| 3737 | this.charLength = 3; |
| 3738 | break; |
| 3739 | } |
| 3740 | |
| 3741 | // 11110XXX |
| 3742 | if (i <= 3 && c >> 3 == 0x1E) { |
| 3743 | this.charLength = 4; |
| 3744 | break; |
| 3745 | } |
| 3746 | } |
| 3747 | |
| 3748 | return i; |
| 3749 | }; |
| 3750 | |
| 3751 | StringDecoder.prototype.end = function(buffer) { |
| 3752 | var res = ''; |
| 3753 | if (buffer && buffer.length) |
| 3754 | res = this.write(buffer); |
| 3755 | |
| 3756 | if (this.charReceived) { |
| 3757 | var cr = this.charReceived; |
| 3758 | var buf = this.charBuffer; |
| 3759 | var enc = this.encoding; |
| 3760 | res += buf.slice(0, cr).toString(enc); |
| 3761 | } |
| 3762 | |
| 3763 | return res; |
| 3764 | }; |
| 3765 | |
| 3766 | function passThroughWrite(buffer) { |
| 3767 | return buffer.toString(this.encoding); |
| 3768 | } |
| 3769 | |
| 3770 | function utf16DetectIncompleteChar(buffer) { |
| 3771 | var incomplete = this.charReceived = buffer.length % 2; |
| 3772 | this.charLength = incomplete ? 2 : 0; |
| 3773 | return incomplete; |
| 3774 | } |
| 3775 | |
| 3776 | function base64DetectIncompleteChar(buffer) { |
| 3777 | var incomplete = this.charReceived = buffer.length % 3; |
| 3778 | this.charLength = incomplete ? 3 : 0; |
| 3779 | return incomplete; |
| 3780 | } |
| 3781 | |
| 3782 | },{"buffer":1}],16:[function(require,module,exports){ |
| 3783 | module.exports = require("./lib/_stream_passthrough.js") |
| 3784 | |
| 3785 | },{"./lib/_stream_passthrough.js":9}],17:[function(require,module,exports){ |
| 3786 | exports = module.exports = require('./lib/_stream_readable.js'); |
| 3787 | exports.Readable = exports; |
| 3788 | exports.Writable = require('./lib/_stream_writable.js'); |
| 3789 | exports.Duplex = require('./lib/_stream_duplex.js'); |
| 3790 | exports.Transform = require('./lib/_stream_transform.js'); |
| 3791 | exports.PassThrough = require('./lib/_stream_passthrough.js'); |
| 3792 | |
| 3793 | },{"./lib/_stream_duplex.js":8,"./lib/_stream_passthrough.js":9,"./lib/_stream_readable.js":10,"./lib/_stream_transform.js":11,"./lib/_stream_writable.js":12}],18:[function(require,module,exports){ |
| 3794 | module.exports = require("./lib/_stream_transform.js") |
| 3795 | |
| 3796 | },{"./lib/_stream_transform.js":11}],19:[function(require,module,exports){ |
| 3797 | module.exports = require("./lib/_stream_writable.js") |
| 3798 | |
| 3799 | },{"./lib/_stream_writable.js":12}],20:[function(require,module,exports){ |
| 3800 | // Copyright Joyent, Inc. and other Node contributors. |
| 3801 | // |
| 3802 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 3803 | // copy of this software and associated documentation files (the |
| 3804 | // "Software"), to deal in the Software without restriction, including |
| 3805 | // without limitation the rights to use, copy, modify, merge, publish, |
| 3806 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 3807 | // persons to whom the Software is furnished to do so, subject to the |
| 3808 | // following conditions: |
| 3809 | // |
| 3810 | // The above copyright notice and this permission notice shall be included |
| 3811 | // in all copies or substantial portions of the Software. |
| 3812 | // |
| 3813 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 3814 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 3815 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 3816 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 3817 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 3818 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 3819 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 3820 | |
| 3821 | module.exports = Stream; |
| 3822 | |
| 3823 | var EE = require('events').EventEmitter; |
| 3824 | var inherits = require('inherits'); |
| 3825 | |
| 3826 | inherits(Stream, EE); |
| 3827 | Stream.Readable = require('readable-stream/readable.js'); |
| 3828 | Stream.Writable = require('readable-stream/writable.js'); |
| 3829 | Stream.Duplex = require('readable-stream/duplex.js'); |
| 3830 | Stream.Transform = require('readable-stream/transform.js'); |
| 3831 | Stream.PassThrough = require('readable-stream/passthrough.js'); |
| 3832 | |
| 3833 | // Backwards-compat with node 0.4.x |
| 3834 | Stream.Stream = Stream; |
| 3835 | |
| 3836 | |
| 3837 | |
| 3838 | // old-style streams. Note that the pipe method (the only relevant |
| 3839 | // part of this class) is overridden in the Readable class. |
| 3840 | |
| 3841 | function Stream() { |
| 3842 | EE.call(this); |
| 3843 | } |
| 3844 | |
| 3845 | Stream.prototype.pipe = function(dest, options) { |
| 3846 | var source = this; |
| 3847 | |
| 3848 | function ondata(chunk) { |
| 3849 | if (dest.writable) { |
| 3850 | if (false === dest.write(chunk) && source.pause) { |
| 3851 | source.pause(); |
| 3852 | } |
| 3853 | } |
| 3854 | } |
| 3855 | |
| 3856 | source.on('data', ondata); |
| 3857 | |
| 3858 | function ondrain() { |
| 3859 | if (source.readable && source.resume) { |
| 3860 | source.resume(); |
| 3861 | } |
| 3862 | } |
| 3863 | |
| 3864 | dest.on('drain', ondrain); |
| 3865 | |
| 3866 | // If the 'end' option is not supplied, dest.end() will be called when |
| 3867 | // source gets the 'end' or 'close' events. Only dest.end() once. |
| 3868 | if (!dest._isStdio && (!options || options.end !== false)) { |
| 3869 | source.on('end', onend); |
| 3870 | source.on('close', onclose); |
| 3871 | } |
| 3872 | |
| 3873 | var didOnEnd = false; |
| 3874 | function onend() { |
| 3875 | if (didOnEnd) return; |
| 3876 | didOnEnd = true; |
| 3877 | |
| 3878 | dest.end(); |
| 3879 | } |
| 3880 | |
| 3881 | |
| 3882 | function onclose() { |
| 3883 | if (didOnEnd) return; |
| 3884 | didOnEnd = true; |
| 3885 | |
| 3886 | if (typeof dest.destroy === 'function') dest.destroy(); |
| 3887 | } |
| 3888 | |
| 3889 | // don't leave dangling pipes when there are errors. |
| 3890 | function onerror(er) { |
| 3891 | cleanup(); |
| 3892 | if (EE.listenerCount(this, 'error') === 0) { |
| 3893 | throw er; // Unhandled stream error in pipe. |
| 3894 | } |
| 3895 | } |
| 3896 | |
| 3897 | source.on('error', onerror); |
| 3898 | dest.on('error', onerror); |
| 3899 | |
| 3900 | // remove all the event listeners that were added. |
| 3901 | function cleanup() { |
| 3902 | source.removeListener('data', ondata); |
| 3903 | dest.removeListener('drain', ondrain); |
| 3904 | |
| 3905 | source.removeListener('end', onend); |
| 3906 | source.removeListener('close', onclose); |
| 3907 | |
| 3908 | source.removeListener('error', onerror); |
| 3909 | dest.removeListener('error', onerror); |
| 3910 | |
| 3911 | source.removeListener('end', cleanup); |
| 3912 | source.removeListener('close', cleanup); |
| 3913 | |
| 3914 | dest.removeListener('close', cleanup); |
| 3915 | } |
| 3916 | |
| 3917 | source.on('end', cleanup); |
| 3918 | source.on('close', cleanup); |
| 3919 | |
| 3920 | dest.on('close', cleanup); |
| 3921 | |
| 3922 | dest.emit('pipe', source); |
| 3923 | |
| 3924 | // Allow for unix-like usage: A.pipe(B).pipe(C) |
| 3925 | return dest; |
| 3926 | }; |
| 3927 | |
| 3928 | },{"events":4,"inherits":5,"readable-stream/duplex.js":7,"readable-stream/passthrough.js":16,"readable-stream/readable.js":17,"readable-stream/transform.js":18,"readable-stream/writable.js":19}],21:[function(require,module,exports){ |
| 3929 | // Copyright Joyent, Inc. and other Node contributors. |
| 3930 | // |
| 3931 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 3932 | // copy of this software and associated documentation files (the |
| 3933 | // "Software"), to deal in the Software without restriction, including |
| 3934 | // without limitation the rights to use, copy, modify, merge, publish, |
| 3935 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 3936 | // persons to whom the Software is furnished to do so, subject to the |
| 3937 | // following conditions: |
| 3938 | // |
| 3939 | // The above copyright notice and this permission notice shall be included |
| 3940 | // in all copies or substantial portions of the Software. |
| 3941 | // |
| 3942 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 3943 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 3944 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 3945 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 3946 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 3947 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 3948 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 3949 | |
| 3950 | var Buffer = require('buffer').Buffer; |
| 3951 | |
| 3952 | function assertEncoding(encoding) { |
| 3953 | if (encoding && !Buffer.isEncoding(encoding)) { |
| 3954 | throw new Error('Unknown encoding: ' + encoding); |
| 3955 | } |
| 3956 | } |
| 3957 | |
| 3958 | var StringDecoder = exports.StringDecoder = function(encoding) { |
| 3959 | this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, ''); |
| 3960 | assertEncoding(encoding); |
| 3961 | switch (this.encoding) { |
| 3962 | case 'utf8': |
| 3963 | // CESU-8 represents each of Surrogate Pair by 3-bytes |
| 3964 | this.surrogateSize = 3; |
| 3965 | break; |
| 3966 | case 'ucs2': |
| 3967 | case 'utf16le': |
| 3968 | // UTF-16 represents each of Surrogate Pair by 2-bytes |
| 3969 | this.surrogateSize = 2; |
| 3970 | this.detectIncompleteChar = utf16DetectIncompleteChar; |
| 3971 | break; |
| 3972 | case 'base64': |
| 3973 | // Base-64 stores 3 bytes in 4 chars, and pads the remainder. |
| 3974 | this.surrogateSize = 3; |
| 3975 | this.detectIncompleteChar = base64DetectIncompleteChar; |
| 3976 | break; |
| 3977 | default: |
| 3978 | this.write = passThroughWrite; |
| 3979 | return; |
| 3980 | } |
| 3981 | |
| 3982 | this.charBuffer = new Buffer(6); |
| 3983 | this.charReceived = 0; |
| 3984 | this.charLength = 0; |
| 3985 | }; |
| 3986 | |
| 3987 | |
| 3988 | StringDecoder.prototype.write = function(buffer) { |
| 3989 | var charStr = ''; |
| 3990 | var offset = 0; |
| 3991 | |
| 3992 | // if our last write ended with an incomplete multibyte character |
| 3993 | while (this.charLength) { |
| 3994 | // determine how many remaining bytes this buffer has to offer for this char |
| 3995 | var i = (buffer.length >= this.charLength - this.charReceived) ? |
| 3996 | this.charLength - this.charReceived : |
| 3997 | buffer.length; |
| 3998 | |
| 3999 | // add the new bytes to the char buffer |
| 4000 | buffer.copy(this.charBuffer, this.charReceived, offset, i); |
| 4001 | this.charReceived += (i - offset); |
| 4002 | offset = i; |
| 4003 | |
| 4004 | if (this.charReceived < this.charLength) { |
| 4005 | // still not enough chars in this buffer? wait for more ... |
| 4006 | return ''; |
| 4007 | } |
| 4008 | |
| 4009 | // get the character that was split |
| 4010 | charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding); |
| 4011 | |
| 4012 | // lead surrogate (D800-DBFF) is also the incomplete character |
| 4013 | var charCode = charStr.charCodeAt(charStr.length - 1); |
| 4014 | if (charCode >= 0xD800 && charCode <= 0xDBFF) { |
| 4015 | this.charLength += this.surrogateSize; |
| 4016 | charStr = ''; |
| 4017 | continue; |
| 4018 | } |
| 4019 | this.charReceived = this.charLength = 0; |
| 4020 | |
| 4021 | // if there are no more bytes in this buffer, just emit our char |
| 4022 | if (i == buffer.length) return charStr; |
| 4023 | |
| 4024 | // otherwise cut off the characters end from the beginning of this buffer |
| 4025 | buffer = buffer.slice(i, buffer.length); |
| 4026 | break; |
| 4027 | } |
| 4028 | |
| 4029 | var lenIncomplete = this.detectIncompleteChar(buffer); |
| 4030 | |
| 4031 | var end = buffer.length; |
| 4032 | if (this.charLength) { |
| 4033 | // buffer the incomplete character bytes we got |
| 4034 | buffer.copy(this.charBuffer, 0, buffer.length - lenIncomplete, end); |
| 4035 | this.charReceived = lenIncomplete; |
| 4036 | end -= lenIncomplete; |
| 4037 | } |
| 4038 | |
| 4039 | charStr += buffer.toString(this.encoding, 0, end); |
| 4040 | |
| 4041 | var end = charStr.length - 1; |
| 4042 | var charCode = charStr.charCodeAt(end); |
| 4043 | // lead surrogate (D800-DBFF) is also the incomplete character |
| 4044 | if (charCode >= 0xD800 && charCode <= 0xDBFF) { |
| 4045 | var size = this.surrogateSize; |
| 4046 | this.charLength += size; |
| 4047 | this.charReceived += size; |
| 4048 | this.charBuffer.copy(this.charBuffer, size, 0, size); |
| 4049 | this.charBuffer.write(charStr.charAt(charStr.length - 1), this.encoding); |
| 4050 | return charStr.substring(0, end); |
| 4051 | } |
| 4052 | |
| 4053 | // or just emit the charStr |
| 4054 | return charStr; |
| 4055 | }; |
| 4056 | |
| 4057 | StringDecoder.prototype.detectIncompleteChar = function(buffer) { |
| 4058 | // determine how many bytes we have to check at the end of this buffer |
| 4059 | var i = (buffer.length >= 3) ? 3 : buffer.length; |
| 4060 | |
| 4061 | // Figure out if one of the last i bytes of our buffer announces an |
| 4062 | // incomplete char. |
| 4063 | for (; i > 0; i--) { |
| 4064 | var c = buffer[buffer.length - i]; |
| 4065 | |
| 4066 | // See http://en.wikipedia.org/wiki/UTF-8#Description |
| 4067 | |
| 4068 | // 110XXXXX |
| 4069 | if (i == 1 && c >> 5 == 0x06) { |
| 4070 | this.charLength = 2; |
| 4071 | break; |
| 4072 | } |
| 4073 | |
| 4074 | // 1110XXXX |
| 4075 | if (i <= 2 && c >> 4 == 0x0E) { |
| 4076 | this.charLength = 3; |
| 4077 | break; |
| 4078 | } |
| 4079 | |
| 4080 | // 11110XXX |
| 4081 | if (i <= 3 && c >> 3 == 0x1E) { |
| 4082 | this.charLength = 4; |
| 4083 | break; |
| 4084 | } |
| 4085 | } |
| 4086 | |
| 4087 | return i; |
| 4088 | }; |
| 4089 | |
| 4090 | StringDecoder.prototype.end = function(buffer) { |
| 4091 | var res = ''; |
| 4092 | if (buffer && buffer.length) |
| 4093 | res = this.write(buffer); |
| 4094 | |
| 4095 | if (this.charReceived) { |
| 4096 | var cr = this.charReceived; |
| 4097 | var buf = this.charBuffer; |
| 4098 | var enc = this.encoding; |
| 4099 | res += buf.slice(0, cr).toString(enc); |
| 4100 | } |
| 4101 | |
| 4102 | return res; |
| 4103 | }; |
| 4104 | |
| 4105 | function passThroughWrite(buffer) { |
| 4106 | return buffer.toString(this.encoding); |
| 4107 | } |
| 4108 | |
| 4109 | function utf16DetectIncompleteChar(buffer) { |
| 4110 | var incomplete = this.charReceived = buffer.length % 2; |
| 4111 | this.charLength = incomplete ? 2 : 0; |
| 4112 | return incomplete; |
| 4113 | } |
| 4114 | |
| 4115 | function base64DetectIncompleteChar(buffer) { |
| 4116 | var incomplete = this.charReceived = buffer.length % 3; |
| 4117 | this.charLength = incomplete ? 3 : 0; |
| 4118 | return incomplete; |
| 4119 | } |
| 4120 | |
| 4121 | },{"buffer":1}],"event-stream":[function(require,module,exports){ |
| 4122 | module.exports=require('ut3LTG'); |
| 4123 | },{}],"ut3LTG":[function(require,module,exports){ |
| 4124 | (function (process,global){ |
| 4125 | //filter will reemit the data if cb(err,pass) pass is truthy |
| 4126 | |
| 4127 | // reduce is more tricky |
| 4128 | // maybe we want to group the reductions or emit progress updates occasionally |
| 4129 | // the most basic reduce just emits one 'data' event after it has recieved 'end' |
| 4130 | |
| 4131 | var Stream = require('stream').Stream |
| 4132 | , es = exports |
| 4133 | , through = require('through') |
| 4134 | , from = require('from') |
| 4135 | , duplex = require('duplexer') |
| 4136 | , map = require('map-stream') |
| 4137 | , pause = require('pause-stream') |
| 4138 | , split = require('split') |
| 4139 | , pipeline = require('stream-combiner') |
| 4140 | , immediately = global.setImmediate || process.nextTick; |
| 4141 | |
| 4142 | es.Stream = Stream //re-export Stream from core |
| 4143 | es.through = through |
| 4144 | es.from = from |
| 4145 | es.duplex = duplex |
| 4146 | es.map = map |
| 4147 | es.pause = pause |
| 4148 | es.split = split |
| 4149 | es.pipeline = es.connect = es.pipe = pipeline |
| 4150 | // merge / concat |
| 4151 | // |
| 4152 | // combine multiple streams into a single stream. |
| 4153 | // will emit end only once |
| 4154 | |
| 4155 | es.concat = //actually this should be called concat |
| 4156 | es.merge = function (/*streams...*/) { |
| 4157 | var toMerge = [].slice.call(arguments) |
| 4158 | var stream = new Stream() |
| 4159 | stream.setMaxListeners(0) // allow adding more than 11 streams |
| 4160 | var endCount = 0 |
| 4161 | stream.writable = stream.readable = true |
| 4162 | |
| 4163 | toMerge.forEach(function (e) { |
| 4164 | e.pipe(stream, {end: false}) |
| 4165 | var ended = false |
| 4166 | e.on('end', function () { |
| 4167 | if(ended) return |
| 4168 | ended = true |
| 4169 | endCount ++ |
| 4170 | if(endCount == toMerge.length) |
| 4171 | stream.emit('end') |
| 4172 | }) |
| 4173 | }) |
| 4174 | stream.write = function (data) { |
| 4175 | this.emit('data', data) |
| 4176 | } |
| 4177 | stream.destroy = function () { |
| 4178 | toMerge.forEach(function (e) { |
| 4179 | if(e.destroy) e.destroy() |
| 4180 | }) |
| 4181 | } |
| 4182 | return stream |
| 4183 | } |
| 4184 | |
| 4185 | |
| 4186 | // writable stream, collects all events into an array |
| 4187 | // and calls back when 'end' occurs |
| 4188 | // mainly I'm using this to test the other functions |
| 4189 | |
| 4190 | es.writeArray = function (done) { |
| 4191 | if ('function' !== typeof done) |
| 4192 | throw new Error('function writeArray (done): done must be function') |
| 4193 | |
| 4194 | var a = new Stream () |
| 4195 | , array = [], isDone = false |
| 4196 | a.write = function (l) { |
| 4197 | array.push(l) |
| 4198 | } |
| 4199 | a.end = function () { |
| 4200 | isDone = true |
| 4201 | done(null, array) |
| 4202 | } |
| 4203 | a.writable = true |
| 4204 | a.readable = false |
| 4205 | a.destroy = function () { |
| 4206 | a.writable = a.readable = false |
| 4207 | if(isDone) return |
| 4208 | done(new Error('destroyed before end'), array) |
| 4209 | } |
| 4210 | return a |
| 4211 | } |
| 4212 | |
| 4213 | //return a Stream that reads the properties of an object |
| 4214 | //respecting pause() and resume() |
| 4215 | |
| 4216 | es.readArray = function (array) { |
| 4217 | var stream = new Stream() |
| 4218 | , i = 0 |
| 4219 | , paused = false |
| 4220 | , ended = false |
| 4221 | |
| 4222 | stream.readable = true |
| 4223 | stream.writable = false |
| 4224 | |
| 4225 | if(!Array.isArray(array)) |
| 4226 | throw new Error('event-stream.read expects an array') |
| 4227 | |
| 4228 | stream.resume = function () { |
| 4229 | if(ended) return |
| 4230 | paused = false |
| 4231 | var l = array.length |
| 4232 | while(i < l && !paused && !ended) { |
| 4233 | stream.emit('data', array[i++]) |
| 4234 | } |
| 4235 | if(i == l && !ended) |
| 4236 | ended = true, stream.readable = false, stream.emit('end') |
| 4237 | } |
| 4238 | process.nextTick(stream.resume) |
| 4239 | stream.pause = function () { |
| 4240 | paused = true |
| 4241 | } |
| 4242 | stream.destroy = function () { |
| 4243 | ended = true |
| 4244 | stream.emit('close') |
| 4245 | } |
| 4246 | return stream |
| 4247 | } |
| 4248 | |
| 4249 | // |
| 4250 | // readable (asyncFunction) |
| 4251 | // return a stream that calls an async function while the stream is not paused. |
| 4252 | // |
| 4253 | // the function must take: (count, callback) {... |
| 4254 | // |
| 4255 | |
| 4256 | es.readable = |
| 4257 | function (func, continueOnError) { |
| 4258 | var stream = new Stream() |
| 4259 | , i = 0 |
| 4260 | , paused = false |
| 4261 | , ended = false |
| 4262 | , reading = false |
| 4263 | |
| 4264 | stream.readable = true |
| 4265 | stream.writable = false |
| 4266 | |
| 4267 | if('function' !== typeof func) |
| 4268 | throw new Error('event-stream.readable expects async function') |
| 4269 | |
| 4270 | stream.on('end', function () { ended = true }) |
| 4271 | |
| 4272 | function get (err, data) { |
| 4273 | |
| 4274 | if(err) { |
| 4275 | stream.emit('error', err) |
| 4276 | if(!continueOnError) stream.emit('end') |
| 4277 | } else if (arguments.length > 1) |
| 4278 | stream.emit('data', data) |
| 4279 | |
| 4280 | immediately(function () { |
| 4281 | if(ended || paused || reading) return |
| 4282 | try { |
| 4283 | reading = true |
| 4284 | func.call(stream, i++, function () { |
| 4285 | reading = false |
| 4286 | get.apply(null, arguments) |
| 4287 | }) |
| 4288 | } catch (err) { |
| 4289 | stream.emit('error', err) |
| 4290 | } |
| 4291 | }) |
| 4292 | } |
| 4293 | stream.resume = function () { |
| 4294 | paused = false |
| 4295 | get() |
| 4296 | } |
| 4297 | process.nextTick(get) |
| 4298 | stream.pause = function () { |
| 4299 | paused = true |
| 4300 | } |
| 4301 | stream.destroy = function () { |
| 4302 | stream.emit('end') |
| 4303 | stream.emit('close') |
| 4304 | ended = true |
| 4305 | } |
| 4306 | return stream |
| 4307 | } |
| 4308 | |
| 4309 | |
| 4310 | // |
| 4311 | // map sync |
| 4312 | // |
| 4313 | |
| 4314 | es.mapSync = function (sync) { |
| 4315 | return es.through(function write(data) { |
| 4316 | var mappedData = sync(data) |
| 4317 | if (typeof mappedData !== 'undefined') |
| 4318 | this.emit('data', mappedData) |
| 4319 | }) |
| 4320 | } |
| 4321 | |
| 4322 | // |
| 4323 | // log just print out what is coming through the stream, for debugging |
| 4324 | // |
| 4325 | |
| 4326 | es.log = function (name) { |
| 4327 | return es.through(function (data) { |
| 4328 | var args = [].slice.call(arguments) |
| 4329 | if(name) console.error(name, data) |
| 4330 | else console.error(data) |
| 4331 | this.emit('data', data) |
| 4332 | }) |
| 4333 | } |
| 4334 | |
| 4335 | |
| 4336 | // |
| 4337 | // child -- pipe through a child process |
| 4338 | // |
| 4339 | |
| 4340 | es.child = function (child) { |
| 4341 | |
| 4342 | return es.duplex(child.stdin, child.stdout) |
| 4343 | |
| 4344 | } |
| 4345 | |
| 4346 | // |
| 4347 | // parse |
| 4348 | // |
| 4349 | // must be used after es.split() to ensure that each chunk represents a line |
| 4350 | // source.pipe(es.split()).pipe(es.parse()) |
| 4351 | |
| 4352 | es.parse = function () { |
| 4353 | return es.through(function (data) { |
| 4354 | var obj |
| 4355 | try { |
| 4356 | if(data) //ignore empty lines |
| 4357 | obj = JSON.parse(data.toString()) |
| 4358 | } catch (err) { |
| 4359 | return console.error(err, 'attemping to parse:', data) |
| 4360 | } |
| 4361 | //ignore lines that where only whitespace. |
| 4362 | if(obj !== undefined) |
| 4363 | this.emit('data', obj) |
| 4364 | }) |
| 4365 | } |
| 4366 | // |
| 4367 | // stringify |
| 4368 | // |
| 4369 | |
| 4370 | es.stringify = function () { |
| 4371 | var Buffer = require('buffer').Buffer |
| 4372 | return es.mapSync(function (e){ |
| 4373 | return JSON.stringify(Buffer.isBuffer(e) ? e.toString() : e) + '\n' |
| 4374 | }) |
| 4375 | } |
| 4376 | |
| 4377 | // |
| 4378 | // replace a string within a stream. |
| 4379 | // |
| 4380 | // warn: just concatenates the string and then does str.split().join(). |
| 4381 | // probably not optimal. |
| 4382 | // for smallish responses, who cares? |
| 4383 | // I need this for shadow-npm so it's only relatively small json files. |
| 4384 | |
| 4385 | es.replace = function (from, to) { |
| 4386 | return es.pipeline(es.split(from), es.join(to)) |
| 4387 | } |
| 4388 | |
| 4389 | // |
| 4390 | // join chunks with a joiner. just like Array#join |
| 4391 | // also accepts a callback that is passed the chunks appended together |
| 4392 | // this is still supported for legacy reasons. |
| 4393 | // |
| 4394 | |
| 4395 | es.join = function (str) { |
| 4396 | |
| 4397 | //legacy api |
| 4398 | if('function' === typeof str) |
| 4399 | return es.wait(str) |
| 4400 | |
| 4401 | var first = true |
| 4402 | return es.through(function (data) { |
| 4403 | if(!first) |
| 4404 | this.emit('data', str) |
| 4405 | first = false |
| 4406 | this.emit('data', data) |
| 4407 | return true |
| 4408 | }) |
| 4409 | } |
| 4410 | |
| 4411 | |
| 4412 | // |
| 4413 | // wait. callback when 'end' is emitted, with all chunks appended as string. |
| 4414 | // |
| 4415 | |
| 4416 | es.wait = function (callback) { |
| 4417 | var body = '' |
| 4418 | return es.through(function (data) { body += data }, |
| 4419 | function () { |
| 4420 | this.emit('data', body) |
| 4421 | this.emit('end') |
| 4422 | if(callback) callback(null, body) |
| 4423 | }) |
| 4424 | } |
| 4425 | |
| 4426 | es.pipeable = function () { |
| 4427 | throw new Error('[EVENT-STREAM] es.pipeable is deprecated') |
| 4428 | } |
| 4429 | |
| 4430 | }).call(this,require("4dON6Z"),typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) |
| 4431 | },{"4dON6Z":6,"buffer":1,"duplexer":24,"from":25,"map-stream":26,"pause-stream":27,"split":28,"stream":20,"stream-combiner":29,"through":30}],24:[function(require,module,exports){ |
| 4432 | var Stream = require("stream") |
| 4433 | var writeMethods = ["write", "end", "destroy"] |
| 4434 | var readMethods = ["resume", "pause"] |
| 4435 | var readEvents = ["data", "close"] |
| 4436 | var slice = Array.prototype.slice |
| 4437 | |
| 4438 | module.exports = duplex |
| 4439 | |
| 4440 | function forEach (arr, fn) { |
| 4441 | if (arr.forEach) { |
| 4442 | return arr.forEach(fn) |
| 4443 | } |
| 4444 | |
| 4445 | for (var i = 0; i < arr.length; i++) { |
| 4446 | fn(arr[i], i) |
| 4447 | } |
| 4448 | } |
| 4449 | |
| 4450 | function duplex(writer, reader) { |
| 4451 | var stream = new Stream() |
| 4452 | var ended = false |
| 4453 | |
| 4454 | forEach(writeMethods, proxyWriter) |
| 4455 | |
| 4456 | forEach(readMethods, proxyReader) |
| 4457 | |
| 4458 | forEach(readEvents, proxyStream) |
| 4459 | |
| 4460 | reader.on("end", handleEnd) |
| 4461 | |
| 4462 | writer.on("drain", function() { |
| 4463 | stream.emit("drain") |
| 4464 | }) |
| 4465 | |
| 4466 | writer.on("error", reemit) |
| 4467 | reader.on("error", reemit) |
| 4468 | |
| 4469 | stream.writable = writer.writable |
| 4470 | stream.readable = reader.readable |
| 4471 | |
| 4472 | return stream |
| 4473 | |
| 4474 | function proxyWriter(methodName) { |
| 4475 | stream[methodName] = method |
| 4476 | |
| 4477 | function method() { |
| 4478 | return writer[methodName].apply(writer, arguments) |
| 4479 | } |
| 4480 | } |
| 4481 | |
| 4482 | function proxyReader(methodName) { |
| 4483 | stream[methodName] = method |
| 4484 | |
| 4485 | function method() { |
| 4486 | stream.emit(methodName) |
| 4487 | var func = reader[methodName] |
| 4488 | if (func) { |
| 4489 | return func.apply(reader, arguments) |
| 4490 | } |
| 4491 | reader.emit(methodName) |
| 4492 | } |
| 4493 | } |
| 4494 | |
| 4495 | function proxyStream(methodName) { |
| 4496 | reader.on(methodName, reemit) |
| 4497 | |
| 4498 | function reemit() { |
| 4499 | var args = slice.call(arguments) |
| 4500 | args.unshift(methodName) |
| 4501 | stream.emit.apply(stream, args) |
| 4502 | } |
| 4503 | } |
| 4504 | |
| 4505 | function handleEnd() { |
| 4506 | if (ended) { |
| 4507 | return |
| 4508 | } |
| 4509 | ended = true |
| 4510 | var args = slice.call(arguments) |
| 4511 | args.unshift("end") |
| 4512 | stream.emit.apply(stream, args) |
| 4513 | } |
| 4514 | |
| 4515 | function reemit(err) { |
| 4516 | stream.emit("error", err) |
| 4517 | } |
| 4518 | } |
| 4519 | |
| 4520 | },{"stream":20}],25:[function(require,module,exports){ |
| 4521 | (function (process){ |
| 4522 | |
| 4523 | 'use strict'; |
| 4524 | |
| 4525 | var Stream = require('stream') |
| 4526 | |
| 4527 | // from |
| 4528 | // |
| 4529 | // a stream that reads from an source. |
| 4530 | // source may be an array, or a function. |
| 4531 | // from handles pause behaviour for you. |
| 4532 | |
| 4533 | module.exports = |
| 4534 | function from (source) { |
| 4535 | if(Array.isArray(source)) { |
| 4536 | source = source.slice() |
| 4537 | return from (function (i) { |
| 4538 | if(source.length) |
| 4539 | this.emit('data', source.shift()) |
| 4540 | else |
| 4541 | this.emit('end') |
| 4542 | return true |
| 4543 | }) |
| 4544 | } |
| 4545 | var s = new Stream(), i = 0 |
| 4546 | s.ended = false |
| 4547 | s.started = false |
| 4548 | s.readable = true |
| 4549 | s.writable = false |
| 4550 | s.paused = false |
| 4551 | s.ended = false |
| 4552 | s.pause = function () { |
| 4553 | s.started = true |
| 4554 | s.paused = true |
| 4555 | } |
| 4556 | function next () { |
| 4557 | s.started = true |
| 4558 | if(s.ended) return |
| 4559 | while(!s.ended && !s.paused && source.call(s, i++, function () { |
| 4560 | if(!s.ended && !s.paused) |
| 4561 | next() |
| 4562 | })) |
| 4563 | ; |
| 4564 | } |
| 4565 | s.resume = function () { |
| 4566 | s.started = true |
| 4567 | s.paused = false |
| 4568 | next() |
| 4569 | } |
| 4570 | s.on('end', function () { |
| 4571 | s.ended = true |
| 4572 | s.readable = false |
| 4573 | process.nextTick(s.destroy) |
| 4574 | }) |
| 4575 | s.destroy = function () { |
| 4576 | s.ended = true |
| 4577 | s.emit('close') |
| 4578 | } |
| 4579 | /* |
| 4580 | by default, the stream will start emitting at nextTick |
| 4581 | if you want, you can pause it, after pipeing. |
| 4582 | you can also resume before next tick, and that will also |
| 4583 | work. |
| 4584 | */ |
| 4585 | process.nextTick(function () { |
| 4586 | if(!s.started) s.resume() |
| 4587 | }) |
| 4588 | return s |
| 4589 | } |
| 4590 | |
| 4591 | }).call(this,require("4dON6Z")) |
| 4592 | },{"4dON6Z":6,"stream":20}],26:[function(require,module,exports){ |
| 4593 | (function (process){ |
| 4594 | //filter will reemit the data if cb(err,pass) pass is truthy |
| 4595 | |
| 4596 | // reduce is more tricky |
| 4597 | // maybe we want to group the reductions or emit progress updates occasionally |
| 4598 | // the most basic reduce just emits one 'data' event after it has recieved 'end' |
| 4599 | |
| 4600 | |
| 4601 | var Stream = require('stream').Stream |
| 4602 | |
| 4603 | |
| 4604 | //create an event stream and apply function to each .write |
| 4605 | //emitting each response as data |
| 4606 | //unless it's an empty callback |
| 4607 | |
| 4608 | module.exports = function (mapper, opts) { |
| 4609 | |
| 4610 | var stream = new Stream() |
| 4611 | , self = this |
| 4612 | , inputs = 0 |
| 4613 | , outputs = 0 |
| 4614 | , ended = false |
| 4615 | , paused = false |
| 4616 | , destroyed = false |
| 4617 | , lastWritten = 0 |
| 4618 | , inNext = false |
| 4619 | |
| 4620 | this.opts = opts || {}; |
| 4621 | var errorEventName = this.opts.failures ? 'failure' : 'error'; |
| 4622 | |
| 4623 | // Items that are not ready to be written yet (because they would come out of |
| 4624 | // order) get stuck in a queue for later. |
| 4625 | var writeQueue = {} |
| 4626 | |
| 4627 | stream.writable = true |
| 4628 | stream.readable = true |
| 4629 | |
| 4630 | function queueData (data, number) { |
| 4631 | var nextToWrite = lastWritten + 1 |
| 4632 | |
| 4633 | if (number === nextToWrite) { |
| 4634 | // If it's next, and its not undefined write it |
| 4635 | if (data !== undefined) { |
| 4636 | stream.emit.apply(stream, ['data', data]) |
| 4637 | } |
| 4638 | lastWritten ++ |
| 4639 | nextToWrite ++ |
| 4640 | } else { |
| 4641 | // Otherwise queue it for later. |
| 4642 | writeQueue[number] = data |
| 4643 | } |
| 4644 | |
| 4645 | // If the next value is in the queue, write it |
| 4646 | if (writeQueue.hasOwnProperty(nextToWrite)) { |
| 4647 | var dataToWrite = writeQueue[nextToWrite] |
| 4648 | delete writeQueue[nextToWrite] |
| 4649 | return queueData(dataToWrite, nextToWrite) |
| 4650 | } |
| 4651 | |
| 4652 | outputs ++ |
| 4653 | if(inputs === outputs) { |
| 4654 | if(paused) paused = false, stream.emit('drain') //written all the incoming events |
| 4655 | if(ended) end() |
| 4656 | } |
| 4657 | } |
| 4658 | |
| 4659 | function next (err, data, number) { |
| 4660 | if(destroyed) return |
| 4661 | inNext = true |
| 4662 | |
| 4663 | if (!err || self.opts.failures) { |
| 4664 | queueData(data, number) |
| 4665 | } |
| 4666 | |
| 4667 | if (err) { |
| 4668 | stream.emit.apply(stream, [ errorEventName, err ]); |
| 4669 | } |
| 4670 | |
| 4671 | inNext = false; |
| 4672 | } |
| 4673 | |
| 4674 | // Wrap the mapper function by calling its callback with the order number of |
| 4675 | // the item in the stream. |
| 4676 | function wrappedMapper (input, number, callback) { |
| 4677 | return mapper.call(null, input, function(err, data){ |
| 4678 | callback(err, data, number) |
| 4679 | }) |
| 4680 | } |
| 4681 | |
| 4682 | stream.write = function (data) { |
| 4683 | if(ended) throw new Error('map stream is not writable') |
| 4684 | inNext = false |
| 4685 | inputs ++ |
| 4686 | |
| 4687 | try { |
| 4688 | //catch sync errors and handle them like async errors |
| 4689 | var written = wrappedMapper(data, inputs, next) |
| 4690 | paused = (written === false) |
| 4691 | return !paused |
| 4692 | } catch (err) { |
| 4693 | //if the callback has been called syncronously, and the error |
| 4694 | //has occured in an listener, throw it again. |
| 4695 | if(inNext) |
| 4696 | throw err |
| 4697 | next(err) |
| 4698 | return !paused |
| 4699 | } |
| 4700 | } |
| 4701 | |
| 4702 | function end (data) { |
| 4703 | //if end was called with args, write it, |
| 4704 | ended = true //write will emit 'end' if ended is true |
| 4705 | stream.writable = false |
| 4706 | if(data !== undefined) { |
| 4707 | return queueData(data, inputs) |
| 4708 | } else if (inputs == outputs) { //wait for processing |
| 4709 | stream.readable = false, stream.emit('end'), stream.destroy() |
| 4710 | } |
| 4711 | } |
| 4712 | |
| 4713 | stream.end = function (data) { |
| 4714 | if(ended) return |
| 4715 | end() |
| 4716 | } |
| 4717 | |
| 4718 | stream.destroy = function () { |
| 4719 | ended = destroyed = true |
| 4720 | stream.writable = stream.readable = paused = false |
| 4721 | process.nextTick(function () { |
| 4722 | stream.emit('close') |
| 4723 | }) |
| 4724 | } |
| 4725 | stream.pause = function () { |
| 4726 | paused = true |
| 4727 | } |
| 4728 | |
| 4729 | stream.resume = function () { |
| 4730 | paused = false |
| 4731 | } |
| 4732 | |
| 4733 | return stream |
| 4734 | } |
| 4735 | |
| 4736 | |
| 4737 | |
| 4738 | |
| 4739 | |
| 4740 | }).call(this,require("4dON6Z")) |
| 4741 | },{"4dON6Z":6,"stream":20}],27:[function(require,module,exports){ |
| 4742 | //through@2 handles this by default! |
| 4743 | module.exports = require('through') |
| 4744 | |
| 4745 | |
| 4746 | },{"through":30}],28:[function(require,module,exports){ |
| 4747 | //filter will reemit the data if cb(err,pass) pass is truthy |
| 4748 | |
| 4749 | // reduce is more tricky |
| 4750 | // maybe we want to group the reductions or emit progress updates occasionally |
| 4751 | // the most basic reduce just emits one 'data' event after it has recieved 'end' |
| 4752 | |
| 4753 | |
| 4754 | var through = require('through') |
| 4755 | var Decoder = require('string_decoder').StringDecoder |
| 4756 | |
| 4757 | module.exports = split |
| 4758 | |
| 4759 | //TODO pass in a function to map across the lines. |
| 4760 | |
| 4761 | function split (matcher, mapper) { |
| 4762 | var decoder = new Decoder() |
| 4763 | var soFar = '' |
| 4764 | if('function' === typeof matcher) |
| 4765 | mapper = matcher, matcher = null |
| 4766 | if (!matcher) |
| 4767 | matcher = /\r?\n/ |
| 4768 | |
| 4769 | function emit(stream, piece) { |
| 4770 | if(mapper) { |
| 4771 | try { |
| 4772 | piece = mapper(piece) |
| 4773 | } |
| 4774 | catch (err) { |
| 4775 | return stream.emit('error', err) |
| 4776 | } |
| 4777 | if('undefined' !== typeof piece) |
| 4778 | stream.queue(piece) |
| 4779 | } |
| 4780 | else |
| 4781 | stream.queue(piece) |
| 4782 | } |
| 4783 | |
| 4784 | function next (stream, buffer) { |
| 4785 | var pieces = (soFar + buffer).split(matcher) |
| 4786 | soFar = pieces.pop() |
| 4787 | |
| 4788 | for (var i = 0; i < pieces.length; i++) { |
| 4789 | var piece = pieces[i] |
| 4790 | emit(stream, piece) |
| 4791 | } |
| 4792 | } |
| 4793 | |
| 4794 | return through(function (b) { |
| 4795 | next(this, decoder.write(b)) |
| 4796 | }, |
| 4797 | function () { |
| 4798 | if(decoder.end) |
| 4799 | next(this, decoder.end()) |
| 4800 | if(soFar != null) |
| 4801 | emit(this, soFar) |
| 4802 | this.queue(null) |
| 4803 | }) |
| 4804 | } |
| 4805 | |
| 4806 | |
| 4807 | },{"string_decoder":21,"through":30}],29:[function(require,module,exports){ |
| 4808 | var duplexer = require('duplexer') |
| 4809 | |
| 4810 | module.exports = function () { |
| 4811 | |
| 4812 | var streams = [].slice.call(arguments) |
| 4813 | , first = streams[0] |
| 4814 | , last = streams[streams.length - 1] |
| 4815 | , thepipe = duplexer(first, last) |
| 4816 | |
| 4817 | if(streams.length == 1) |
| 4818 | return streams[0] |
| 4819 | else if (!streams.length) |
| 4820 | throw new Error('connect called with empty args') |
| 4821 | |
| 4822 | //pipe all the streams together |
| 4823 | |
| 4824 | function recurse (streams) { |
| 4825 | if(streams.length < 2) |
| 4826 | return |
| 4827 | streams[0].pipe(streams[1]) |
| 4828 | recurse(streams.slice(1)) |
| 4829 | } |
| 4830 | |
| 4831 | recurse(streams) |
| 4832 | |
| 4833 | function onerror () { |
| 4834 | var args = [].slice.call(arguments) |
| 4835 | args.unshift('error') |
| 4836 | thepipe.emit.apply(thepipe, args) |
| 4837 | } |
| 4838 | |
| 4839 | //es.duplex already reemits the error from the first and last stream. |
| 4840 | //add a listener for the inner streams in the pipeline. |
| 4841 | for(var i = 1; i < streams.length - 1; i ++) |
| 4842 | streams[i].on('error', onerror) |
| 4843 | |
| 4844 | return thepipe |
| 4845 | } |
| 4846 | |
| 4847 | |
| 4848 | },{"duplexer":24}],30:[function(require,module,exports){ |
| 4849 | (function (process){ |
| 4850 | var Stream = require('stream') |
| 4851 | |
| 4852 | // through |
| 4853 | // |
| 4854 | // a stream that does nothing but re-emit the input. |
| 4855 | // useful for aggregating a series of changing but not ending streams into one stream) |
| 4856 | |
| 4857 | exports = module.exports = through |
| 4858 | through.through = through |
| 4859 | |
| 4860 | //create a readable writable stream. |
| 4861 | |
| 4862 | function through (write, end, opts) { |
| 4863 | write = write || function (data) { this.queue(data) } |
| 4864 | end = end || function () { this.queue(null) } |
| 4865 | |
| 4866 | var ended = false, destroyed = false, buffer = [], _ended = false |
| 4867 | var stream = new Stream() |
| 4868 | stream.readable = stream.writable = true |
| 4869 | stream.paused = false |
| 4870 | |
| 4871 | // stream.autoPause = !(opts && opts.autoPause === false) |
| 4872 | stream.autoDestroy = !(opts && opts.autoDestroy === false) |
| 4873 | |
| 4874 | stream.write = function (data) { |
| 4875 | write.call(this, data) |
| 4876 | return !stream.paused |
| 4877 | } |
| 4878 | |
| 4879 | function drain() { |
| 4880 | while(buffer.length && !stream.paused) { |
| 4881 | var data = buffer.shift() |
| 4882 | if(null === data) |
| 4883 | return stream.emit('end') |
| 4884 | else |
| 4885 | stream.emit('data', data) |
| 4886 | } |
| 4887 | } |
| 4888 | |
| 4889 | stream.queue = stream.push = function (data) { |
| 4890 | // console.error(ended) |
| 4891 | if(_ended) return stream |
| 4892 | if(data == null) _ended = true |
| 4893 | buffer.push(data) |
| 4894 | drain() |
| 4895 | return stream |
| 4896 | } |
| 4897 | |
| 4898 | //this will be registered as the first 'end' listener |
| 4899 | //must call destroy next tick, to make sure we're after any |
| 4900 | //stream piped from here. |
| 4901 | //this is only a problem if end is not emitted synchronously. |
| 4902 | //a nicer way to do this is to make sure this is the last listener for 'end' |
| 4903 | |
| 4904 | stream.on('end', function () { |
| 4905 | stream.readable = false |
| 4906 | if(!stream.writable && stream.autoDestroy) |
| 4907 | process.nextTick(function () { |
| 4908 | stream.destroy() |
| 4909 | }) |
| 4910 | }) |
| 4911 | |
| 4912 | function _end () { |
| 4913 | stream.writable = false |
| 4914 | end.call(stream) |
| 4915 | if(!stream.readable && stream.autoDestroy) |
| 4916 | stream.destroy() |
| 4917 | } |
| 4918 | |
| 4919 | stream.end = function (data) { |
| 4920 | if(ended) return |
| 4921 | ended = true |
| 4922 | if(arguments.length) stream.write(data) |
| 4923 | _end() // will emit or queue |
| 4924 | return stream |
| 4925 | } |
| 4926 | |
| 4927 | stream.destroy = function () { |
| 4928 | if(destroyed) return |
| 4929 | destroyed = true |
| 4930 | ended = true |
| 4931 | buffer.length = 0 |
| 4932 | stream.writable = stream.readable = false |
| 4933 | stream.emit('close') |
| 4934 | return stream |
| 4935 | } |
| 4936 | |
| 4937 | stream.pause = function () { |
| 4938 | if(stream.paused) return |
| 4939 | stream.paused = true |
| 4940 | return stream |
| 4941 | } |
| 4942 | |
| 4943 | stream.resume = function () { |
| 4944 | if(stream.paused) { |
| 4945 | stream.paused = false |
| 4946 | stream.emit('resume') |
| 4947 | } |
| 4948 | drain() |
| 4949 | //may have become paused again, |
| 4950 | //as drain emits 'data'. |
| 4951 | if(!stream.paused) |
| 4952 | stream.emit('drain') |
| 4953 | return stream |
| 4954 | } |
| 4955 | return stream |
| 4956 | } |
| 4957 | |
| 4958 | |
| 4959 | }).call(this,require("4dON6Z")) |
| 4960 | },{"4dON6Z":6,"stream":20}]},{},[]) |