diff options
author | Ian Coleman <ian@iancoleman.io> | 2018-05-08 09:42:36 +1000 |
---|---|---|
committer | Ian Coleman <ian@iancoleman.io> | 2018-05-08 10:19:59 +1000 |
commit | e0f91e20297bfd528550d6505b3e87a0e31c16a0 (patch) | |
tree | 13d7e9fb8b89b9da147771a26c427287536dec35 /src/js | |
parent | 956e44efd75941d7b6613a947de663923d5be7b3 (diff) | |
download | BIP39-e0f91e20297bfd528550d6505b3e87a0e31c16a0.tar.gz BIP39-e0f91e20297bfd528550d6505b3e87a0e31c16a0.tar.zst BIP39-e0f91e20297bfd528550d6505b3e87a0e31c16a0.zip |
Add cashaddr address format for bitcoin cash
Diffstat (limited to 'src/js')
-rw-r--r-- | src/js/bchaddrjs-0.2.1.js | 9328 | ||||
-rw-r--r-- | src/js/bitcoinjs-extensions.js | 11 | ||||
-rw-r--r-- | src/js/index.js | 37 |
3 files changed, 9344 insertions, 32 deletions
diff --git a/src/js/bchaddrjs-0.2.1.js b/src/js/bchaddrjs-0.2.1.js new file mode 100644 index 0000000..8356f10 --- /dev/null +++ b/src/js/bchaddrjs-0.2.1.js | |||
@@ -0,0 +1,9328 @@ | |||
1 | (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.bchaddr = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
2 | // base-x encoding | ||
3 | // Forked from https://github.com/cryptocoinjs/bs58 | ||
4 | // Originally written by Mike Hearn for BitcoinJ | ||
5 | // Copyright (c) 2011 Google Inc | ||
6 | // Ported to JavaScript by Stefan Thomas | ||
7 | // Merged Buffer refactorings from base58-native by Stephen Pair | ||
8 | // Copyright (c) 2013 BitPay Inc | ||
9 | |||
10 | var Buffer = require('safe-buffer').Buffer | ||
11 | |||
12 | module.exports = function base (ALPHABET) { | ||
13 | var ALPHABET_MAP = {} | ||
14 | var BASE = ALPHABET.length | ||
15 | var LEADER = ALPHABET.charAt(0) | ||
16 | |||
17 | // pre-compute lookup table | ||
18 | for (var z = 0; z < ALPHABET.length; z++) { | ||
19 | var x = ALPHABET.charAt(z) | ||
20 | |||
21 | if (ALPHABET_MAP[x] !== undefined) throw new TypeError(x + ' is ambiguous') | ||
22 | ALPHABET_MAP[x] = z | ||
23 | } | ||
24 | |||
25 | function encode (source) { | ||
26 | if (source.length === 0) return '' | ||
27 | |||
28 | var digits = [0] | ||
29 | for (var i = 0; i < source.length; ++i) { | ||
30 | for (var j = 0, carry = source[i]; j < digits.length; ++j) { | ||
31 | carry += digits[j] << 8 | ||
32 | digits[j] = carry % BASE | ||
33 | carry = (carry / BASE) | 0 | ||
34 | } | ||
35 | |||
36 | while (carry > 0) { | ||
37 | digits.push(carry % BASE) | ||
38 | carry = (carry / BASE) | 0 | ||
39 | } | ||
40 | } | ||
41 | |||
42 | var string = '' | ||
43 | |||
44 | // deal with leading zeros | ||
45 | for (var k = 0; source[k] === 0 && k < source.length - 1; ++k) string += LEADER | ||
46 | // convert digits to a string | ||
47 | for (var q = digits.length - 1; q >= 0; --q) string += ALPHABET[digits[q]] | ||
48 | |||
49 | return string | ||
50 | } | ||
51 | |||
52 | function decodeUnsafe (string) { | ||
53 | if (typeof string !== 'string') throw new TypeError('Expected String') | ||
54 | if (string.length === 0) return Buffer.allocUnsafe(0) | ||
55 | |||
56 | var bytes = [0] | ||
57 | for (var i = 0; i < string.length; i++) { | ||
58 | var value = ALPHABET_MAP[string[i]] | ||
59 | if (value === undefined) return | ||
60 | |||
61 | for (var j = 0, carry = value; j < bytes.length; ++j) { | ||
62 | carry += bytes[j] * BASE | ||
63 | bytes[j] = carry & 0xff | ||
64 | carry >>= 8 | ||
65 | } | ||
66 | |||
67 | while (carry > 0) { | ||
68 | bytes.push(carry & 0xff) | ||
69 | carry >>= 8 | ||
70 | } | ||
71 | } | ||
72 | |||
73 | // deal with leading zeros | ||
74 | for (var k = 0; string[k] === LEADER && k < string.length - 1; ++k) { | ||
75 | bytes.push(0) | ||
76 | } | ||
77 | |||
78 | return Buffer.from(bytes.reverse()) | ||
79 | } | ||
80 | |||
81 | function decode (string) { | ||
82 | var buffer = decodeUnsafe(string) | ||
83 | if (buffer) return buffer | ||
84 | |||
85 | throw new Error('Non-base' + BASE + ' character') | ||
86 | } | ||
87 | |||
88 | return { | ||
89 | encode: encode, | ||
90 | decodeUnsafe: decodeUnsafe, | ||
91 | decode: decode | ||
92 | } | ||
93 | } | ||
94 | |||
95 | },{"safe-buffer":40}],2:[function(require,module,exports){ | ||
96 | 'use strict' | ||
97 | |||
98 | exports.byteLength = byteLength | ||
99 | exports.toByteArray = toByteArray | ||
100 | exports.fromByteArray = fromByteArray | ||
101 | |||
102 | var lookup = [] | ||
103 | var revLookup = [] | ||
104 | var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array | ||
105 | |||
106 | var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' | ||
107 | for (var i = 0, len = code.length; i < len; ++i) { | ||
108 | lookup[i] = code[i] | ||
109 | revLookup[code.charCodeAt(i)] = i | ||
110 | } | ||
111 | |||
112 | revLookup['-'.charCodeAt(0)] = 62 | ||
113 | revLookup['_'.charCodeAt(0)] = 63 | ||
114 | |||
115 | function placeHoldersCount (b64) { | ||
116 | var len = b64.length | ||
117 | if (len % 4 > 0) { | ||
118 | throw new Error('Invalid string. Length must be a multiple of 4') | ||
119 | } | ||
120 | |||
121 | // the number of equal signs (place holders) | ||
122 | // if there are two placeholders, than the two characters before it | ||
123 | // represent one byte | ||
124 | // if there is only one, then the three characters before it represent 2 bytes | ||
125 | // this is just a cheap hack to not do indexOf twice | ||
126 | return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 | ||
127 | } | ||
128 | |||
129 | function byteLength (b64) { | ||
130 | // base64 is 4/3 + up to two characters of the original data | ||
131 | return (b64.length * 3 / 4) - placeHoldersCount(b64) | ||
132 | } | ||
133 | |||
134 | function toByteArray (b64) { | ||
135 | var i, l, tmp, placeHolders, arr | ||
136 | var len = b64.length | ||
137 | placeHolders = placeHoldersCount(b64) | ||
138 | |||
139 | arr = new Arr((len * 3 / 4) - placeHolders) | ||
140 | |||
141 | // if there are placeholders, only get up to the last complete 4 chars | ||
142 | l = placeHolders > 0 ? len - 4 : len | ||
143 | |||
144 | var L = 0 | ||
145 | |||
146 | for (i = 0; i < l; i += 4) { | ||
147 | tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)] | ||
148 | arr[L++] = (tmp >> 16) & 0xFF | ||
149 | arr[L++] = (tmp >> 8) & 0xFF | ||
150 | arr[L++] = tmp & 0xFF | ||
151 | } | ||
152 | |||
153 | if (placeHolders === 2) { | ||
154 | tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4) | ||
155 | arr[L++] = tmp & 0xFF | ||
156 | } else if (placeHolders === 1) { | ||
157 | tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2) | ||
158 | arr[L++] = (tmp >> 8) & 0xFF | ||
159 | arr[L++] = tmp & 0xFF | ||
160 | } | ||
161 | |||
162 | return arr | ||
163 | } | ||
164 | |||
165 | function tripletToBase64 (num) { | ||
166 | return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] | ||
167 | } | ||
168 | |||
169 | function encodeChunk (uint8, start, end) { | ||
170 | var tmp | ||
171 | var output = [] | ||
172 | for (var i = start; i < end; i += 3) { | ||
173 | tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) | ||
174 | output.push(tripletToBase64(tmp)) | ||
175 | } | ||
176 | return output.join('') | ||
177 | } | ||
178 | |||
179 | function fromByteArray (uint8) { | ||
180 | var tmp | ||
181 | var len = uint8.length | ||
182 | var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes | ||
183 | var output = '' | ||
184 | var parts = [] | ||
185 | var maxChunkLength = 16383 // must be multiple of 3 | ||
186 | |||
187 | // go through the array every three bytes, we'll deal with trailing stuff later | ||
188 | for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { | ||
189 | parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))) | ||
190 | } | ||
191 | |||
192 | // pad the end with zeros, but make sure to not forget the extra bytes | ||
193 | if (extraBytes === 1) { | ||
194 | tmp = uint8[len - 1] | ||
195 | output += lookup[tmp >> 2] | ||
196 | output += lookup[(tmp << 4) & 0x3F] | ||
197 | output += '==' | ||
198 | } else if (extraBytes === 2) { | ||
199 | tmp = (uint8[len - 2] << 8) + (uint8[len - 1]) | ||
200 | output += lookup[tmp >> 10] | ||
201 | output += lookup[(tmp >> 4) & 0x3F] | ||
202 | output += lookup[(tmp << 2) & 0x3F] | ||
203 | output += '=' | ||
204 | } | ||
205 | |||
206 | parts.push(output) | ||
207 | |||
208 | return parts.join('') | ||
209 | } | ||
210 | |||
211 | },{}],3:[function(require,module,exports){ | ||
212 | var bigInt = (function (undefined) { | ||
213 | "use strict"; | ||
214 | |||
215 | var BASE = 1e7, | ||
216 | LOG_BASE = 7, | ||
217 | MAX_INT = 9007199254740992, | ||
218 | MAX_INT_ARR = smallToArray(MAX_INT), | ||
219 | LOG_MAX_INT = Math.log(MAX_INT); | ||
220 | |||
221 | function Integer(v, radix) { | ||
222 | if (typeof v === "undefined") return Integer[0]; | ||
223 | if (typeof radix !== "undefined") return +radix === 10 ? parseValue(v) : parseBase(v, radix); | ||
224 | return parseValue(v); | ||
225 | } | ||
226 | |||
227 | function BigInteger(value, sign) { | ||
228 | this.value = value; | ||
229 | this.sign = sign; | ||
230 | this.isSmall = false; | ||
231 | } | ||
232 | BigInteger.prototype = Object.create(Integer.prototype); | ||
233 | |||
234 | function SmallInteger(value) { | ||
235 | this.value = value; | ||
236 | this.sign = value < 0; | ||
237 | this.isSmall = true; | ||
238 | } | ||
239 | SmallInteger.prototype = Object.create(Integer.prototype); | ||
240 | |||
241 | function isPrecise(n) { | ||
242 | return -MAX_INT < n && n < MAX_INT; | ||
243 | } | ||
244 | |||
245 | function smallToArray(n) { // For performance reasons doesn't reference BASE, need to change this function if BASE changes | ||
246 | if (n < 1e7) | ||
247 | return [n]; | ||
248 | if (n < 1e14) | ||
249 | return [n % 1e7, Math.floor(n / 1e7)]; | ||
250 | return [n % 1e7, Math.floor(n / 1e7) % 1e7, Math.floor(n / 1e14)]; | ||
251 | } | ||
252 | |||
253 | function arrayToSmall(arr) { // If BASE changes this function may need to change | ||
254 | trim(arr); | ||
255 | var length = arr.length; | ||
256 | if (length < 4 && compareAbs(arr, MAX_INT_ARR) < 0) { | ||
257 | switch (length) { | ||
258 | case 0: return 0; | ||
259 | case 1: return arr[0]; | ||
260 | case 2: return arr[0] + arr[1] * BASE; | ||
261 | default: return arr[0] + (arr[1] + arr[2] * BASE) * BASE; | ||
262 | } | ||
263 | } | ||
264 | return arr; | ||
265 | } | ||
266 | |||
267 | function trim(v) { | ||
268 | var i = v.length; | ||
269 | while (v[--i] === 0); | ||
270 | v.length = i + 1; | ||
271 | } | ||
272 | |||
273 | function createArray(length) { // function shamelessly stolen from Yaffle's library https://github.com/Yaffle/BigInteger | ||
274 | var x = new Array(length); | ||
275 | var i = -1; | ||
276 | while (++i < length) { | ||
277 | x[i] = 0; | ||
278 | } | ||
279 | return x; | ||
280 | } | ||
281 | |||
282 | function truncate(n) { | ||
283 | if (n > 0) return Math.floor(n); | ||
284 | return Math.ceil(n); | ||
285 | } | ||
286 | |||
287 | function add(a, b) { // assumes a and b are arrays with a.length >= b.length | ||
288 | var l_a = a.length, | ||
289 | l_b = b.length, | ||
290 | r = new Array(l_a), | ||
291 | carry = 0, | ||
292 | base = BASE, | ||
293 | sum, i; | ||
294 | for (i = 0; i < l_b; i++) { | ||
295 | sum = a[i] + b[i] + carry; | ||
296 | carry = sum >= base ? 1 : 0; | ||
297 | r[i] = sum - carry * base; | ||
298 | } | ||
299 | while (i < l_a) { | ||
300 | sum = a[i] + carry; | ||
301 | carry = sum === base ? 1 : 0; | ||
302 | r[i++] = sum - carry * base; | ||
303 | } | ||
304 | if (carry > 0) r.push(carry); | ||
305 | return r; | ||
306 | } | ||
307 | |||
308 | function addAny(a, b) { | ||
309 | if (a.length >= b.length) return add(a, b); | ||
310 | return add(b, a); | ||
311 | } | ||
312 | |||
313 | function addSmall(a, carry) { // assumes a is array, carry is number with 0 <= carry < MAX_INT | ||
314 | var l = a.length, | ||
315 | r = new Array(l), | ||
316 | base = BASE, | ||
317 | sum, i; | ||
318 | for (i = 0; i < l; i++) { | ||
319 | sum = a[i] - base + carry; | ||
320 | carry = Math.floor(sum / base); | ||
321 | r[i] = sum - carry * base; | ||
322 | carry += 1; | ||
323 | } | ||
324 | while (carry > 0) { | ||
325 | r[i++] = carry % base; | ||
326 | carry = Math.floor(carry / base); | ||
327 | } | ||
328 | return r; | ||
329 | } | ||
330 | |||
331 | BigInteger.prototype.add = function (v) { | ||
332 | var n = parseValue(v); | ||
333 | if (this.sign !== n.sign) { | ||
334 | return this.subtract(n.negate()); | ||
335 | } | ||
336 | var a = this.value, b = n.value; | ||
337 | if (n.isSmall) { | ||
338 | return new BigInteger(addSmall(a, Math.abs(b)), this.sign); | ||
339 | } | ||
340 | return new BigInteger(addAny(a, b), this.sign); | ||
341 | }; | ||
342 | BigInteger.prototype.plus = BigInteger.prototype.add; | ||
343 | |||
344 | SmallInteger.prototype.add = function (v) { | ||
345 | var n = parseValue(v); | ||
346 | var a = this.value; | ||
347 | if (a < 0 !== n.sign) { | ||
348 | return this.subtract(n.negate()); | ||
349 | } | ||
350 | var b = n.value; | ||
351 | if (n.isSmall) { | ||
352 | if (isPrecise(a + b)) return new SmallInteger(a + b); | ||
353 | b = smallToArray(Math.abs(b)); | ||
354 | } | ||
355 | return new BigInteger(addSmall(b, Math.abs(a)), a < 0); | ||
356 | }; | ||
357 | SmallInteger.prototype.plus = SmallInteger.prototype.add; | ||
358 | |||
359 | function subtract(a, b) { // assumes a and b are arrays with a >= b | ||
360 | var a_l = a.length, | ||
361 | b_l = b.length, | ||
362 | r = new Array(a_l), | ||
363 | borrow = 0, | ||
364 | base = BASE, | ||
365 | i, difference; | ||
366 | for (i = 0; i < b_l; i++) { | ||
367 | difference = a[i] - borrow - b[i]; | ||
368 | if (difference < 0) { | ||
369 | difference += base; | ||
370 | borrow = 1; | ||
371 | } else borrow = 0; | ||
372 | r[i] = difference; | ||
373 | } | ||
374 | for (i = b_l; i < a_l; i++) { | ||
375 | difference = a[i] - borrow; | ||
376 | if (difference < 0) difference += base; | ||
377 | else { | ||
378 | r[i++] = difference; | ||
379 | break; | ||
380 | } | ||
381 | r[i] = difference; | ||
382 | } | ||
383 | for (; i < a_l; i++) { | ||
384 | r[i] = a[i]; | ||
385 | } | ||
386 | trim(r); | ||
387 | return r; | ||
388 | } | ||
389 | |||
390 | function subtractAny(a, b, sign) { | ||
391 | var value; | ||
392 | if (compareAbs(a, b) >= 0) { | ||
393 | value = subtract(a,b); | ||
394 | } else { | ||
395 | value = subtract(b, a); | ||
396 | sign = !sign; | ||
397 | } | ||
398 | value = arrayToSmall(value); | ||
399 | if (typeof value === "number") { | ||
400 | if (sign) value = -value; | ||
401 | return new SmallInteger(value); | ||
402 | } | ||
403 | return new BigInteger(value, sign); | ||
404 | } | ||
405 | |||
406 | function subtractSmall(a, b, sign) { // assumes a is array, b is number with 0 <= b < MAX_INT | ||
407 | var l = a.length, | ||
408 | r = new Array(l), | ||
409 | carry = -b, | ||
410 | base = BASE, | ||
411 | i, difference; | ||
412 | for (i = 0; i < l; i++) { | ||
413 | difference = a[i] + carry; | ||
414 | carry = Math.floor(difference / base); | ||
415 | difference %= base; | ||
416 | r[i] = difference < 0 ? difference + base : difference; | ||
417 | } | ||
418 | r = arrayToSmall(r); | ||
419 | if (typeof r === "number") { | ||
420 | if (sign) r = -r; | ||
421 | return new SmallInteger(r); | ||
422 | } return new BigInteger(r, sign); | ||
423 | } | ||
424 | |||
425 | BigInteger.prototype.subtract = function (v) { | ||
426 | var n = parseValue(v); | ||
427 | if (this.sign !== n.sign) { | ||
428 | return this.add(n.negate()); | ||
429 | } | ||
430 | var a = this.value, b = n.value; | ||
431 | if (n.isSmall) | ||
432 | return subtractSmall(a, Math.abs(b), this.sign); | ||
433 | return subtractAny(a, b, this.sign); | ||
434 | }; | ||
435 | BigInteger.prototype.minus = BigInteger.prototype.subtract; | ||
436 | |||
437 | SmallInteger.prototype.subtract = function (v) { | ||
438 | var n = parseValue(v); | ||
439 | var a = this.value; | ||
440 | if (a < 0 !== n.sign) { | ||
441 | return this.add(n.negate()); | ||
442 | } | ||
443 | var b = n.value; | ||
444 | if (n.isSmall) { | ||
445 | return new SmallInteger(a - b); | ||
446 | } | ||
447 | return subtractSmall(b, Math.abs(a), a >= 0); | ||
448 | }; | ||
449 | SmallInteger.prototype.minus = SmallInteger.prototype.subtract; | ||
450 | |||
451 | BigInteger.prototype.negate = function () { | ||
452 | return new BigInteger(this.value, !this.sign); | ||
453 | }; | ||
454 | SmallInteger.prototype.negate = function () { | ||
455 | var sign = this.sign; | ||
456 | var small = new SmallInteger(-this.value); | ||
457 | small.sign = !sign; | ||
458 | return small; | ||
459 | }; | ||
460 | |||
461 | BigInteger.prototype.abs = function () { | ||
462 | return new BigInteger(this.value, false); | ||
463 | }; | ||
464 | SmallInteger.prototype.abs = function () { | ||
465 | return new SmallInteger(Math.abs(this.value)); | ||
466 | }; | ||
467 | |||
468 | function multiplyLong(a, b) { | ||
469 | var a_l = a.length, | ||
470 | b_l = b.length, | ||
471 | l = a_l + b_l, | ||
472 | r = createArray(l), | ||
473 | base = BASE, | ||
474 | product, carry, i, a_i, b_j; | ||
475 | for (i = 0; i < a_l; ++i) { | ||
476 | a_i = a[i]; | ||
477 | for (var j = 0; j < b_l; ++j) { | ||
478 | b_j = b[j]; | ||
479 | product = a_i * b_j + r[i + j]; | ||
480 | carry = Math.floor(product / base); | ||
481 | r[i + j] = product - carry * base; | ||
482 | r[i + j + 1] += carry; | ||
483 | } | ||
484 | } | ||
485 | trim(r); | ||
486 | return r; | ||
487 | } | ||
488 | |||
489 | function multiplySmall(a, b) { // assumes a is array, b is number with |b| < BASE | ||
490 | var l = a.length, | ||
491 | r = new Array(l), | ||
492 | base = BASE, | ||
493 | carry = 0, | ||
494 | product, i; | ||
495 | for (i = 0; i < l; i++) { | ||
496 | product = a[i] * b + carry; | ||
497 | carry = Math.floor(product / base); | ||
498 | r[i] = product - carry * base; | ||
499 | } | ||
500 | while (carry > 0) { | ||
501 | r[i++] = carry % base; | ||
502 | carry = Math.floor(carry / base); | ||
503 | } | ||
504 | return r; | ||
505 | } | ||
506 | |||
507 | function shiftLeft(x, n) { | ||
508 | var r = []; | ||
509 | while (n-- > 0) r.push(0); | ||
510 | return r.concat(x); | ||
511 | } | ||
512 | |||
513 | function multiplyKaratsuba(x, y) { | ||
514 | var n = Math.max(x.length, y.length); | ||
515 | |||
516 | if (n <= 30) return multiplyLong(x, y); | ||
517 | n = Math.ceil(n / 2); | ||
518 | |||
519 | var b = x.slice(n), | ||
520 | a = x.slice(0, n), | ||
521 | d = y.slice(n), | ||
522 | c = y.slice(0, n); | ||
523 | |||
524 | var ac = multiplyKaratsuba(a, c), | ||
525 | bd = multiplyKaratsuba(b, d), | ||
526 | abcd = multiplyKaratsuba(addAny(a, b), addAny(c, d)); | ||
527 | |||
528 | var product = addAny(addAny(ac, shiftLeft(subtract(subtract(abcd, ac), bd), n)), shiftLeft(bd, 2 * n)); | ||
529 | trim(product); | ||
530 | return product; | ||
531 | } | ||
532 | |||
533 | // The following function is derived from a surface fit of a graph plotting the performance difference | ||
534 | // between long multiplication and karatsuba multiplication versus the lengths of the two arrays. | ||
535 | function useKaratsuba(l1, l2) { | ||
536 | return -0.012 * l1 - 0.012 * l2 + 0.000015 * l1 * l2 > 0; | ||
537 | } | ||
538 | |||
539 | BigInteger.prototype.multiply = function (v) { | ||
540 | var n = parseValue(v), | ||
541 | a = this.value, b = n.value, | ||
542 | sign = this.sign !== n.sign, | ||
543 | abs; | ||
544 | if (n.isSmall) { | ||
545 | if (b === 0) return Integer[0]; | ||
546 | if (b === 1) return this; | ||
547 | if (b === -1) return this.negate(); | ||
548 | abs = Math.abs(b); | ||
549 | if (abs < BASE) { | ||
550 | return new BigInteger(multiplySmall(a, abs), sign); | ||
551 | } | ||
552 | b = smallToArray(abs); | ||
553 | } | ||
554 | if (useKaratsuba(a.length, b.length)) // Karatsuba is only faster for certain array sizes | ||
555 | return new BigInteger(multiplyKaratsuba(a, b), sign); | ||
556 | return new BigInteger(multiplyLong(a, b), sign); | ||
557 | }; | ||
558 | |||
559 | BigInteger.prototype.times = BigInteger.prototype.multiply; | ||
560 | |||
561 | function multiplySmallAndArray(a, b, sign) { // a >= 0 | ||
562 | if (a < BASE) { | ||
563 | return new BigInteger(multiplySmall(b, a), sign); | ||
564 | } | ||
565 | return new BigInteger(multiplyLong(b, smallToArray(a)), sign); | ||
566 | } | ||
567 | SmallInteger.prototype._multiplyBySmall = function (a) { | ||
568 | if (isPrecise(a.value * this.value)) { | ||
569 | return new SmallInteger(a.value * this.value); | ||
570 | } | ||
571 | return multiplySmallAndArray(Math.abs(a.value), smallToArray(Math.abs(this.value)), this.sign !== a.sign); | ||
572 | }; | ||
573 | BigInteger.prototype._multiplyBySmall = function (a) { | ||
574 | if (a.value === 0) return Integer[0]; | ||
575 | if (a.value === 1) return this; | ||
576 | if (a.value === -1) return this.negate(); | ||
577 | return multiplySmallAndArray(Math.abs(a.value), this.value, this.sign !== a.sign); | ||
578 | }; | ||
579 | SmallInteger.prototype.multiply = function (v) { | ||
580 | return parseValue(v)._multiplyBySmall(this); | ||
581 | }; | ||
582 | SmallInteger.prototype.times = SmallInteger.prototype.multiply; | ||
583 | |||
584 | function square(a) { | ||
585 | var l = a.length, | ||
586 | r = createArray(l + l), | ||
587 | base = BASE, | ||
588 | product, carry, i, a_i, a_j; | ||
589 | for (i = 0; i < l; i++) { | ||
590 | a_i = a[i]; | ||
591 | for (var j = 0; j < l; j++) { | ||
592 | a_j = a[j]; | ||
593 | product = a_i * a_j + r[i + j]; | ||
594 | carry = Math.floor(product / base); | ||
595 | r[i + j] = product - carry * base; | ||
596 | r[i + j + 1] += carry; | ||
597 | } | ||
598 | } | ||
599 | trim(r); | ||
600 | return r; | ||
601 | } | ||
602 | |||
603 | BigInteger.prototype.square = function () { | ||
604 | return new BigInteger(square(this.value), false); | ||
605 | }; | ||
606 | |||
607 | SmallInteger.prototype.square = function () { | ||
608 | var value = this.value * this.value; | ||
609 | if (isPrecise(value)) return new SmallInteger(value); | ||
610 | return new BigInteger(square(smallToArray(Math.abs(this.value))), false); | ||
611 | }; | ||
612 | |||
613 | function divMod1(a, b) { // Left over from previous version. Performs faster than divMod2 on smaller input sizes. | ||
614 | var a_l = a.length, | ||
615 | b_l = b.length, | ||
616 | base = BASE, | ||
617 | result = createArray(b.length), | ||
618 | divisorMostSignificantDigit = b[b_l - 1], | ||
619 | // normalization | ||
620 | lambda = Math.ceil(base / (2 * divisorMostSignificantDigit)), | ||
621 | remainder = multiplySmall(a, lambda), | ||
622 | divisor = multiplySmall(b, lambda), | ||
623 | quotientDigit, shift, carry, borrow, i, l, q; | ||
624 | if (remainder.length <= a_l) remainder.push(0); | ||
625 | divisor.push(0); | ||
626 | divisorMostSignificantDigit = divisor[b_l - 1]; | ||
627 | for (shift = a_l - b_l; shift >= 0; shift--) { | ||
628 | quotientDigit = base - 1; | ||
629 | if (remainder[shift + b_l] !== divisorMostSignificantDigit) { | ||
630 | quotientDigit = Math.floor((remainder[shift + b_l] * base + remainder[shift + b_l - 1]) / divisorMostSignificantDigit); | ||
631 | } | ||
632 | // quotientDigit <= base - 1 | ||
633 | carry = 0; | ||
634 | borrow = 0; | ||
635 | l = divisor.length; | ||
636 | for (i = 0; i < l; i++) { | ||
637 | carry += quotientDigit * divisor[i]; | ||
638 | q = Math.floor(carry / base); | ||
639 | borrow += remainder[shift + i] - (carry - q * base); | ||
640 | carry = q; | ||
641 | if (borrow < 0) { | ||
642 | remainder[shift + i] = borrow + base; | ||
643 | borrow = -1; | ||
644 | } else { | ||
645 | remainder[shift + i] = borrow; | ||
646 | borrow = 0; | ||
647 | } | ||
648 | } | ||
649 | while (borrow !== 0) { | ||
650 | quotientDigit -= 1; | ||
651 | carry = 0; | ||
652 | for (i = 0; i < l; i++) { | ||
653 | carry += remainder[shift + i] - base + divisor[i]; | ||
654 | if (carry < 0) { | ||
655 | remainder[shift + i] = carry + base; | ||
656 | carry = 0; | ||
657 | } else { | ||
658 | remainder[shift + i] = carry; | ||
659 | carry = 1; | ||
660 | } | ||
661 | } | ||
662 | borrow += carry; | ||
663 | } | ||
664 | result[shift] = quotientDigit; | ||
665 | } | ||
666 | // denormalization | ||
667 | remainder = divModSmall(remainder, lambda)[0]; | ||
668 | return [arrayToSmall(result), arrayToSmall(remainder)]; | ||
669 | } | ||
670 | |||
671 | function divMod2(a, b) { // Implementation idea shamelessly stolen from Silent Matt's library http://silentmatt.com/biginteger/ | ||
672 | // Performs faster than divMod1 on larger input sizes. | ||
673 | var a_l = a.length, | ||
674 | b_l = b.length, | ||
675 | result = [], | ||
676 | part = [], | ||
677 | base = BASE, | ||
678 | guess, xlen, highx, highy, check; | ||
679 | while (a_l) { | ||
680 | part.unshift(a[--a_l]); | ||
681 | trim(part); | ||
682 | if (compareAbs(part, b) < 0) { | ||
683 | result.push(0); | ||
684 | continue; | ||
685 | } | ||
686 | xlen = part.length; | ||
687 | highx = part[xlen - 1] * base + part[xlen - 2]; | ||
688 | highy = b[b_l - 1] * base + b[b_l - 2]; | ||
689 | if (xlen > b_l) { | ||
690 | highx = (highx + 1) * base; | ||
691 | } | ||
692 | guess = Math.ceil(highx / highy); | ||
693 | do { | ||
694 | check = multiplySmall(b, guess); | ||
695 | if (compareAbs(check, part) <= 0) break; | ||
696 | guess--; | ||
697 | } while (guess); | ||
698 | result.push(guess); | ||
699 | part = subtract(part, check); | ||
700 | } | ||
701 | result.reverse(); | ||
702 | return [arrayToSmall(result), arrayToSmall(part)]; | ||
703 | } | ||
704 | |||
705 | function divModSmall(value, lambda) { | ||
706 | var length = value.length, | ||
707 | quotient = createArray(length), | ||
708 | base = BASE, | ||
709 | i, q, remainder, divisor; | ||
710 | remainder = 0; | ||
711 | for (i = length - 1; i >= 0; --i) { | ||
712 | divisor = remainder * base + value[i]; | ||
713 | q = truncate(divisor / lambda); | ||
714 | remainder = divisor - q * lambda; | ||
715 | quotient[i] = q | 0; | ||
716 | } | ||
717 | return [quotient, remainder | 0]; | ||
718 | } | ||
719 | |||
720 | function divModAny(self, v) { | ||
721 | var value, n = parseValue(v); | ||
722 | var a = self.value, b = n.value; | ||
723 | var quotient; | ||
724 | if (b === 0) throw new Error("Cannot divide by zero"); | ||
725 | if (self.isSmall) { | ||
726 | if (n.isSmall) { | ||
727 | return [new SmallInteger(truncate(a / b)), new SmallInteger(a % b)]; | ||
728 | } | ||
729 | return [Integer[0], self]; | ||
730 | } | ||
731 | if (n.isSmall) { | ||
732 | if (b === 1) return [self, Integer[0]]; | ||
733 | if (b == -1) return [self.negate(), Integer[0]]; | ||
734 | var abs = Math.abs(b); | ||
735 | if (abs < BASE) { | ||
736 | value = divModSmall(a, abs); | ||
737 | quotient = arrayToSmall(value[0]); | ||
738 | var remainder = value[1]; | ||
739 | if (self.sign) remainder = -remainder; | ||
740 | if (typeof quotient === "number") { | ||
741 | if (self.sign !== n.sign) quotient = -quotient; | ||
742 | return [new SmallInteger(quotient), new SmallInteger(remainder)]; | ||
743 | } | ||
744 | return [new BigInteger(quotient, self.sign !== n.sign), new SmallInteger(remainder)]; | ||
745 | } | ||
746 | b = smallToArray(abs); | ||
747 | } | ||
748 | var comparison = compareAbs(a, b); | ||
749 | if (comparison === -1) return [Integer[0], self]; | ||
750 | if (comparison === 0) return [Integer[self.sign === n.sign ? 1 : -1], Integer[0]]; | ||
751 | |||
752 | // divMod1 is faster on smaller input sizes | ||
753 | if (a.length + b.length <= 200) | ||
754 | value = divMod1(a, b); | ||
755 | else value = divMod2(a, b); | ||
756 | |||
757 | quotient = value[0]; | ||
758 | var qSign = self.sign !== n.sign, | ||
759 | mod = value[1], | ||
760 | mSign = self.sign; | ||
761 | if (typeof quotient === "number") { | ||
762 | if (qSign) quotient = -quotient; | ||
763 | quotient = new SmallInteger(quotient); | ||
764 | } else quotient = new BigInteger(quotient, qSign); | ||
765 | if (typeof mod === "number") { | ||
766 | if (mSign) mod = -mod; | ||
767 | mod = new SmallInteger(mod); | ||
768 | } else mod = new BigInteger(mod, mSign); | ||
769 | return [quotient, mod]; | ||
770 | } | ||
771 | |||
772 | BigInteger.prototype.divmod = function (v) { | ||
773 | var result = divModAny(this, v); | ||
774 | return { | ||
775 | quotient: result[0], | ||
776 | remainder: result[1] | ||
777 | }; | ||
778 | }; | ||
779 | SmallInteger.prototype.divmod = BigInteger.prototype.divmod; | ||
780 | |||
781 | BigInteger.prototype.divide = function (v) { | ||
782 | return divModAny(this, v)[0]; | ||
783 | }; | ||
784 | SmallInteger.prototype.over = SmallInteger.prototype.divide = BigInteger.prototype.over = BigInteger.prototype.divide; | ||
785 | |||
786 | BigInteger.prototype.mod = function (v) { | ||
787 | return divModAny(this, v)[1]; | ||
788 | }; | ||
789 | SmallInteger.prototype.remainder = SmallInteger.prototype.mod = BigInteger.prototype.remainder = BigInteger.prototype.mod; | ||
790 | |||
791 | BigInteger.prototype.pow = function (v) { | ||
792 | var n = parseValue(v), | ||
793 | a = this.value, | ||
794 | b = n.value, | ||
795 | value, x, y; | ||
796 | if (b === 0) return Integer[1]; | ||
797 | if (a === 0) return Integer[0]; | ||
798 | if (a === 1) return Integer[1]; | ||
799 | if (a === -1) return n.isEven() ? Integer[1] : Integer[-1]; | ||
800 | if (n.sign) { | ||
801 | return Integer[0]; | ||
802 | } | ||
803 | if (!n.isSmall) throw new Error("The exponent " + n.toString() + " is too large."); | ||
804 | if (this.isSmall) { | ||
805 | if (isPrecise(value = Math.pow(a, b))) | ||
806 | return new SmallInteger(truncate(value)); | ||
807 | } | ||
808 | x = this; | ||
809 | y = Integer[1]; | ||
810 | while (true) { | ||
811 | if (b & 1 === 1) { | ||
812 | y = y.times(x); | ||
813 | --b; | ||
814 | } | ||
815 | if (b === 0) break; | ||
816 | b /= 2; | ||
817 | x = x.square(); | ||
818 | } | ||
819 | return y; | ||
820 | }; | ||
821 | SmallInteger.prototype.pow = BigInteger.prototype.pow; | ||
822 | |||
823 | BigInteger.prototype.modPow = function (exp, mod) { | ||
824 | exp = parseValue(exp); | ||
825 | mod = parseValue(mod); | ||
826 | if (mod.isZero()) throw new Error("Cannot take modPow with modulus 0"); | ||
827 | var r = Integer[1], | ||
828 | base = this.mod(mod); | ||
829 | while (exp.isPositive()) { | ||
830 | if (base.isZero()) return Integer[0]; | ||
831 | if (exp.isOdd()) r = r.multiply(base).mod(mod); | ||
832 | exp = exp.divide(2); | ||
833 | base = base.square().mod(mod); | ||
834 | } | ||
835 | return r; | ||
836 | }; | ||
837 | SmallInteger.prototype.modPow = BigInteger.prototype.modPow; | ||
838 | |||
839 | function compareAbs(a, b) { | ||
840 | if (a.length !== b.length) { | ||
841 | return a.length > b.length ? 1 : -1; | ||
842 | } | ||
843 | for (var i = a.length - 1; i >= 0; i--) { | ||
844 | if (a[i] !== b[i]) return a[i] > b[i] ? 1 : -1; | ||
845 | } | ||
846 | return 0; | ||
847 | } | ||
848 | |||
849 | BigInteger.prototype.compareAbs = function (v) { | ||
850 | var n = parseValue(v), | ||
851 | a = this.value, | ||
852 | b = n.value; | ||
853 | if (n.isSmall) return 1; | ||
854 | return compareAbs(a, b); | ||
855 | }; | ||
856 | SmallInteger.prototype.compareAbs = function (v) { | ||
857 | var n = parseValue(v), | ||
858 | a = Math.abs(this.value), | ||
859 | b = n.value; | ||
860 | if (n.isSmall) { | ||
861 | b = Math.abs(b); | ||
862 | return a === b ? 0 : a > b ? 1 : -1; | ||
863 | } | ||
864 | return -1; | ||
865 | }; | ||
866 | |||
867 | BigInteger.prototype.compare = function (v) { | ||
868 | // See discussion about comparison with Infinity: | ||
869 | // https://github.com/peterolson/BigInteger.js/issues/61 | ||
870 | if (v === Infinity) { | ||
871 | return -1; | ||
872 | } | ||
873 | if (v === -Infinity) { | ||
874 | return 1; | ||
875 | } | ||
876 | |||
877 | var n = parseValue(v), | ||
878 | a = this.value, | ||
879 | b = n.value; | ||
880 | if (this.sign !== n.sign) { | ||
881 | return n.sign ? 1 : -1; | ||
882 | } | ||
883 | if (n.isSmall) { | ||
884 | return this.sign ? -1 : 1; | ||
885 | } | ||
886 | return compareAbs(a, b) * (this.sign ? -1 : 1); | ||
887 | }; | ||
888 | BigInteger.prototype.compareTo = BigInteger.prototype.compare; | ||
889 | |||
890 | SmallInteger.prototype.compare = function (v) { | ||
891 | if (v === Infinity) { | ||
892 | return -1; | ||
893 | } | ||
894 | if (v === -Infinity) { | ||
895 | return 1; | ||
896 | } | ||
897 | |||
898 | var n = parseValue(v), | ||
899 | a = this.value, | ||
900 | b = n.value; | ||
901 | if (n.isSmall) { | ||
902 | return a == b ? 0 : a > b ? 1 : -1; | ||
903 | } | ||
904 | if (a < 0 !== n.sign) { | ||
905 | return a < 0 ? -1 : 1; | ||
906 | } | ||
907 | return a < 0 ? 1 : -1; | ||
908 | }; | ||
909 | SmallInteger.prototype.compareTo = SmallInteger.prototype.compare; | ||
910 | |||
911 | BigInteger.prototype.equals = function (v) { | ||
912 | return this.compare(v) === 0; | ||
913 | }; | ||
914 | SmallInteger.prototype.eq = SmallInteger.prototype.equals = BigInteger.prototype.eq = BigInteger.prototype.equals; | ||
915 | |||
916 | BigInteger.prototype.notEquals = function (v) { | ||
917 | return this.compare(v) !== 0; | ||
918 | }; | ||
919 | SmallInteger.prototype.neq = SmallInteger.prototype.notEquals = BigInteger.prototype.neq = BigInteger.prototype.notEquals; | ||
920 | |||
921 | BigInteger.prototype.greater = function (v) { | ||
922 | return this.compare(v) > 0; | ||
923 | }; | ||
924 | SmallInteger.prototype.gt = SmallInteger.prototype.greater = BigInteger.prototype.gt = BigInteger.prototype.greater; | ||
925 | |||
926 | BigInteger.prototype.lesser = function (v) { | ||
927 | return this.compare(v) < 0; | ||
928 | }; | ||
929 | SmallInteger.prototype.lt = SmallInteger.prototype.lesser = BigInteger.prototype.lt = BigInteger.prototype.lesser; | ||
930 | |||
931 | BigInteger.prototype.greaterOrEquals = function (v) { | ||
932 | return this.compare(v) >= 0; | ||
933 | }; | ||
934 | SmallInteger.prototype.geq = SmallInteger.prototype.greaterOrEquals = BigInteger.prototype.geq = BigInteger.prototype.greaterOrEquals; | ||
935 | |||
936 | BigInteger.prototype.lesserOrEquals = function (v) { | ||
937 | return this.compare(v) <= 0; | ||
938 | }; | ||
939 | SmallInteger.prototype.leq = SmallInteger.prototype.lesserOrEquals = BigInteger.prototype.leq = BigInteger.prototype.lesserOrEquals; | ||
940 | |||
941 | BigInteger.prototype.isEven = function () { | ||
942 | return (this.value[0] & 1) === 0; | ||
943 | }; | ||
944 | SmallInteger.prototype.isEven = function () { | ||
945 | return (this.value & 1) === 0; | ||
946 | }; | ||
947 | |||
948 | BigInteger.prototype.isOdd = function () { | ||
949 | return (this.value[0] & 1) === 1; | ||
950 | }; | ||
951 | SmallInteger.prototype.isOdd = function () { | ||
952 | return (this.value & 1) === 1; | ||
953 | }; | ||
954 | |||
955 | BigInteger.prototype.isPositive = function () { | ||
956 | return !this.sign; | ||
957 | }; | ||
958 | SmallInteger.prototype.isPositive = function () { | ||
959 | return this.value > 0; | ||
960 | }; | ||
961 | |||
962 | BigInteger.prototype.isNegative = function () { | ||
963 | return this.sign; | ||
964 | }; | ||
965 | SmallInteger.prototype.isNegative = function () { | ||
966 | return this.value < 0; | ||
967 | }; | ||
968 | |||
969 | BigInteger.prototype.isUnit = function () { | ||
970 | return false; | ||
971 | }; | ||
972 | SmallInteger.prototype.isUnit = function () { | ||
973 | return Math.abs(this.value) === 1; | ||
974 | }; | ||
975 | |||
976 | BigInteger.prototype.isZero = function () { | ||
977 | return false; | ||
978 | }; | ||
979 | SmallInteger.prototype.isZero = function () { | ||
980 | return this.value === 0; | ||
981 | }; | ||
982 | BigInteger.prototype.isDivisibleBy = function (v) { | ||
983 | var n = parseValue(v); | ||
984 | var value = n.value; | ||
985 | if (value === 0) return false; | ||
986 | if (value === 1) return true; | ||
987 | if (value === 2) return this.isEven(); | ||
988 | return this.mod(n).equals(Integer[0]); | ||
989 | }; | ||
990 | SmallInteger.prototype.isDivisibleBy = BigInteger.prototype.isDivisibleBy; | ||
991 | |||
992 | function isBasicPrime(v) { | ||
993 | var n = v.abs(); | ||
994 | if (n.isUnit()) return false; | ||
995 | if (n.equals(2) || n.equals(3) || n.equals(5)) return true; | ||
996 | if (n.isEven() || n.isDivisibleBy(3) || n.isDivisibleBy(5)) return false; | ||
997 | if (n.lesser(25)) return true; | ||
998 | // we don't know if it's prime: let the other functions figure it out | ||
999 | } | ||
1000 | |||
1001 | BigInteger.prototype.isPrime = function () { | ||
1002 | var isPrime = isBasicPrime(this); | ||
1003 | if (isPrime !== undefined) return isPrime; | ||
1004 | var n = this.abs(), | ||
1005 | nPrev = n.prev(); | ||
1006 | var a = [2, 3, 5, 7, 11, 13, 17, 19], | ||
1007 | b = nPrev, | ||
1008 | d, t, i, x; | ||
1009 | while (b.isEven()) b = b.divide(2); | ||
1010 | for (i = 0; i < a.length; i++) { | ||
1011 | x = bigInt(a[i]).modPow(b, n); | ||
1012 | if (x.equals(Integer[1]) || x.equals(nPrev)) continue; | ||
1013 | for (t = true, d = b; t && d.lesser(nPrev) ; d = d.multiply(2)) { | ||
1014 | x = x.square().mod(n); | ||
1015 | if (x.equals(nPrev)) t = false; | ||
1016 | } | ||
1017 | if (t) return false; | ||
1018 | } | ||
1019 | return true; | ||
1020 | }; | ||
1021 | SmallInteger.prototype.isPrime = BigInteger.prototype.isPrime; | ||
1022 | |||
1023 | BigInteger.prototype.isProbablePrime = function (iterations) { | ||
1024 | var isPrime = isBasicPrime(this); | ||
1025 | if (isPrime !== undefined) return isPrime; | ||
1026 | var n = this.abs(); | ||
1027 | var t = iterations === undefined ? 5 : iterations; | ||
1028 | // use the Fermat primality test | ||
1029 | for (var i = 0; i < t; i++) { | ||
1030 | var a = bigInt.randBetween(2, n.minus(2)); | ||
1031 | if (!a.modPow(n.prev(), n).isUnit()) return false; // definitely composite | ||
1032 | } | ||
1033 | return true; // large chance of being prime | ||
1034 | }; | ||
1035 | SmallInteger.prototype.isProbablePrime = BigInteger.prototype.isProbablePrime; | ||
1036 | |||
1037 | BigInteger.prototype.modInv = function (n) { | ||
1038 | var t = bigInt.zero, newT = bigInt.one, r = parseValue(n), newR = this.abs(), q, lastT, lastR; | ||
1039 | while (!newR.equals(bigInt.zero)) { | ||
1040 | q = r.divide(newR); | ||
1041 | lastT = t; | ||
1042 | lastR = r; | ||
1043 | t = newT; | ||
1044 | r = newR; | ||
1045 | newT = lastT.subtract(q.multiply(newT)); | ||
1046 | newR = lastR.subtract(q.multiply(newR)); | ||
1047 | } | ||
1048 | if (!r.equals(1)) throw new Error(this.toString() + " and " + n.toString() + " are not co-prime"); | ||
1049 | if (t.compare(0) === -1) { | ||
1050 | t = t.add(n); | ||
1051 | } | ||
1052 | if (this.isNegative()) { | ||
1053 | return t.negate(); | ||
1054 | } | ||
1055 | return t; | ||
1056 | }; | ||
1057 | |||
1058 | SmallInteger.prototype.modInv = BigInteger.prototype.modInv; | ||
1059 | |||
1060 | BigInteger.prototype.next = function () { | ||
1061 | var value = this.value; | ||
1062 | if (this.sign) { | ||
1063 | return subtractSmall(value, 1, this.sign); | ||
1064 | } | ||
1065 | return new BigInteger(addSmall(value, 1), this.sign); | ||
1066 | }; | ||
1067 | SmallInteger.prototype.next = function () { | ||
1068 | var value = this.value; | ||
1069 | if (value + 1 < MAX_INT) return new SmallInteger(value + 1); | ||
1070 | return new BigInteger(MAX_INT_ARR, false); | ||
1071 | }; | ||
1072 | |||
1073 | BigInteger.prototype.prev = function () { | ||
1074 | var value = this.value; | ||
1075 | if (this.sign) { | ||
1076 | return new BigInteger(addSmall(value, 1), true); | ||
1077 | } | ||
1078 | return subtractSmall(value, 1, this.sign); | ||
1079 | }; | ||
1080 | SmallInteger.prototype.prev = function () { | ||
1081 | var value = this.value; | ||
1082 | if (value - 1 > -MAX_INT) return new SmallInteger(value - 1); | ||
1083 | return new BigInteger(MAX_INT_ARR, true); | ||
1084 | }; | ||
1085 | |||
1086 | var powersOfTwo = [1]; | ||
1087 | while (2 * powersOfTwo[powersOfTwo.length - 1] <= BASE) powersOfTwo.push(2 * powersOfTwo[powersOfTwo.length - 1]); | ||
1088 | var powers2Length = powersOfTwo.length, highestPower2 = powersOfTwo[powers2Length - 1]; | ||
1089 | |||
1090 | function shift_isSmall(n) { | ||
1091 | return ((typeof n === "number" || typeof n === "string") && +Math.abs(n) <= BASE) || | ||
1092 | (n instanceof BigInteger && n.value.length <= 1); | ||
1093 | } | ||
1094 | |||
1095 | BigInteger.prototype.shiftLeft = function (n) { | ||
1096 | if (!shift_isSmall(n)) { | ||
1097 | throw new Error(String(n) + " is too large for shifting."); | ||
1098 | } | ||
1099 | n = +n; | ||
1100 | if (n < 0) return this.shiftRight(-n); | ||
1101 | var result = this; | ||
1102 | while (n >= powers2Length) { | ||
1103 | result = result.multiply(highestPower2); | ||
1104 | n -= powers2Length - 1; | ||
1105 | } | ||
1106 | return result.multiply(powersOfTwo[n]); | ||
1107 | }; | ||
1108 | SmallInteger.prototype.shiftLeft = BigInteger.prototype.shiftLeft; | ||
1109 | |||
1110 | BigInteger.prototype.shiftRight = function (n) { | ||
1111 | var remQuo; | ||
1112 | if (!shift_isSmall(n)) { | ||
1113 | throw new Error(String(n) + " is too large for shifting."); | ||
1114 | } | ||
1115 | n = +n; | ||
1116 | if (n < 0) return this.shiftLeft(-n); | ||
1117 | var result = this; | ||
1118 | while (n >= powers2Length) { | ||
1119 | if (result.isZero()) return result; | ||
1120 | remQuo = divModAny(result, highestPower2); | ||
1121 | result = remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0]; | ||
1122 | n -= powers2Length - 1; | ||
1123 | } | ||
1124 | remQuo = divModAny(result, powersOfTwo[n]); | ||
1125 | return remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0]; | ||
1126 | }; | ||
1127 | SmallInteger.prototype.shiftRight = BigInteger.prototype.shiftRight; | ||
1128 | |||
1129 | function bitwise(x, y, fn) { | ||
1130 | y = parseValue(y); | ||
1131 | var xSign = x.isNegative(), ySign = y.isNegative(); | ||
1132 | var xRem = xSign ? x.not() : x, | ||
1133 | yRem = ySign ? y.not() : y; | ||
1134 | var xDigit = 0, yDigit = 0; | ||
1135 | var xDivMod = null, yDivMod = null; | ||
1136 | var result = []; | ||
1137 | while (!xRem.isZero() || !yRem.isZero()) { | ||
1138 | xDivMod = divModAny(xRem, highestPower2); | ||
1139 | xDigit = xDivMod[1].toJSNumber(); | ||
1140 | if (xSign) { | ||
1141 | xDigit = highestPower2 - 1 - xDigit; // two's complement for negative numbers | ||
1142 | } | ||
1143 | |||
1144 | yDivMod = divModAny(yRem, highestPower2); | ||
1145 | yDigit = yDivMod[1].toJSNumber(); | ||
1146 | if (ySign) { | ||
1147 | yDigit = highestPower2 - 1 - yDigit; // two's complement for negative numbers | ||
1148 | } | ||
1149 | |||
1150 | xRem = xDivMod[0]; | ||
1151 | yRem = yDivMod[0]; | ||
1152 | result.push(fn(xDigit, yDigit)); | ||
1153 | } | ||
1154 | var sum = fn(xSign ? 1 : 0, ySign ? 1 : 0) !== 0 ? bigInt(-1) : bigInt(0); | ||
1155 | for (var i = result.length - 1; i >= 0; i -= 1) { | ||
1156 | sum = sum.multiply(highestPower2).add(bigInt(result[i])); | ||
1157 | } | ||
1158 | return sum; | ||
1159 | } | ||
1160 | |||
1161 | BigInteger.prototype.not = function () { | ||
1162 | return this.negate().prev(); | ||
1163 | }; | ||
1164 | SmallInteger.prototype.not = BigInteger.prototype.not; | ||
1165 | |||
1166 | BigInteger.prototype.and = function (n) { | ||
1167 | return bitwise(this, n, function (a, b) { return a & b; }); | ||
1168 | }; | ||
1169 | SmallInteger.prototype.and = BigInteger.prototype.and; | ||
1170 | |||
1171 | BigInteger.prototype.or = function (n) { | ||
1172 | return bitwise(this, n, function (a, b) { return a | b; }); | ||
1173 | }; | ||
1174 | SmallInteger.prototype.or = BigInteger.prototype.or; | ||
1175 | |||
1176 | BigInteger.prototype.xor = function (n) { | ||
1177 | return bitwise(this, n, function (a, b) { return a ^ b; }); | ||
1178 | }; | ||
1179 | SmallInteger.prototype.xor = BigInteger.prototype.xor; | ||
1180 | |||
1181 | var LOBMASK_I = 1 << 30, LOBMASK_BI = (BASE & -BASE) * (BASE & -BASE) | LOBMASK_I; | ||
1182 | function roughLOB(n) { // get lowestOneBit (rough) | ||
1183 | // SmallInteger: return Min(lowestOneBit(n), 1 << 30) | ||
1184 | // BigInteger: return Min(lowestOneBit(n), 1 << 14) [BASE=1e7] | ||
1185 | var v = n.value, x = typeof v === "number" ? v | LOBMASK_I : v[0] + v[1] * BASE | LOBMASK_BI; | ||
1186 | return x & -x; | ||
1187 | } | ||
1188 | |||
1189 | function max(a, b) { | ||
1190 | a = parseValue(a); | ||
1191 | b = parseValue(b); | ||
1192 | return a.greater(b) ? a : b; | ||
1193 | } | ||
1194 | function min(a, b) { | ||
1195 | a = parseValue(a); | ||
1196 | b = parseValue(b); | ||
1197 | return a.lesser(b) ? a : b; | ||
1198 | } | ||
1199 | function gcd(a, b) { | ||
1200 | a = parseValue(a).abs(); | ||
1201 | b = parseValue(b).abs(); | ||
1202 | if (a.equals(b)) return a; | ||
1203 | if (a.isZero()) return b; | ||
1204 | if (b.isZero()) return a; | ||
1205 | var c = Integer[1], d, t; | ||
1206 | while (a.isEven() && b.isEven()) { | ||
1207 | d = Math.min(roughLOB(a), roughLOB(b)); | ||
1208 | a = a.divide(d); | ||
1209 | b = b.divide(d); | ||
1210 | c = c.multiply(d); | ||
1211 | } | ||
1212 | while (a.isEven()) { | ||
1213 | a = a.divide(roughLOB(a)); | ||
1214 | } | ||
1215 | do { | ||
1216 | while (b.isEven()) { | ||
1217 | b = b.divide(roughLOB(b)); | ||
1218 | } | ||
1219 | if (a.greater(b)) { | ||
1220 | t = b; b = a; a = t; | ||
1221 | } | ||
1222 | b = b.subtract(a); | ||
1223 | } while (!b.isZero()); | ||
1224 | return c.isUnit() ? a : a.multiply(c); | ||
1225 | } | ||
1226 | function lcm(a, b) { | ||
1227 | a = parseValue(a).abs(); | ||
1228 | b = parseValue(b).abs(); | ||
1229 | return a.divide(gcd(a, b)).multiply(b); | ||
1230 | } | ||
1231 | function randBetween(a, b) { | ||
1232 | a = parseValue(a); | ||
1233 | b = parseValue(b); | ||
1234 | var low = min(a, b), high = max(a, b); | ||
1235 | var range = high.subtract(low).add(1); | ||
1236 | if (range.isSmall) return low.add(Math.floor(Math.random() * range)); | ||
1237 | var length = range.value.length - 1; | ||
1238 | var result = [], restricted = true; | ||
1239 | for (var i = length; i >= 0; i--) { | ||
1240 | var top = restricted ? range.value[i] : BASE; | ||
1241 | var digit = truncate(Math.random() * top); | ||
1242 | result.unshift(digit); | ||
1243 | if (digit < top) restricted = false; | ||
1244 | } | ||
1245 | result = arrayToSmall(result); | ||
1246 | return low.add(typeof result === "number" ? new SmallInteger(result) : new BigInteger(result, false)); | ||
1247 | } | ||
1248 | var parseBase = function (text, base) { | ||
1249 | var length = text.length; | ||
1250 | var i; | ||
1251 | var absBase = Math.abs(base); | ||
1252 | for(var i = 0; i < length; i++) { | ||
1253 | var c = text[i].toLowerCase(); | ||
1254 | if(c === "-") continue; | ||
1255 | if(/[a-z0-9]/.test(c)) { | ||
1256 | if(/[0-9]/.test(c) && +c >= absBase) { | ||
1257 | if(c === "1" && absBase === 1) continue; | ||
1258 | throw new Error(c + " is not a valid digit in base " + base + "."); | ||
1259 | } else if(c.charCodeAt(0) - 87 >= absBase) { | ||
1260 | throw new Error(c + " is not a valid digit in base " + base + "."); | ||
1261 | } | ||
1262 | } | ||
1263 | } | ||
1264 | if (2 <= base && base <= 36) { | ||
1265 | if (length <= LOG_MAX_INT / Math.log(base)) { | ||
1266 | var result = parseInt(text, base); | ||
1267 | if(isNaN(result)) { | ||
1268 | throw new Error(c + " is not a valid digit in base " + base + "."); | ||
1269 | } | ||
1270 | return new SmallInteger(parseInt(text, base)); | ||
1271 | } | ||
1272 | } | ||
1273 | base = parseValue(base); | ||
1274 | var digits = []; | ||
1275 | var isNegative = text[0] === "-"; | ||
1276 | for (i = isNegative ? 1 : 0; i < text.length; i++) { | ||
1277 | var c = text[i].toLowerCase(), | ||
1278 | charCode = c.charCodeAt(0); | ||
1279 | if (48 <= charCode && charCode <= 57) digits.push(parseValue(c)); | ||
1280 | else if (97 <= charCode && charCode <= 122) digits.push(parseValue(c.charCodeAt(0) - 87)); | ||
1281 | else if (c === "<") { | ||
1282 | var start = i; | ||
1283 | do { i++; } while (text[i] !== ">"); | ||
1284 | digits.push(parseValue(text.slice(start + 1, i))); | ||
1285 | } | ||
1286 | else throw new Error(c + " is not a valid character"); | ||
1287 | } | ||
1288 | return parseBaseFromArray(digits, base, isNegative); | ||
1289 | }; | ||
1290 | |||
1291 | function parseBaseFromArray(digits, base, isNegative) { | ||
1292 | var val = Integer[0], pow = Integer[1], i; | ||
1293 | for (i = digits.length - 1; i >= 0; i--) { | ||
1294 | val = val.add(digits[i].times(pow)); | ||
1295 | pow = pow.times(base); | ||
1296 | } | ||
1297 | return isNegative ? val.negate() : val; | ||
1298 | } | ||
1299 | |||
1300 | function stringify(digit) { | ||
1301 | var v = digit.value; | ||
1302 | if (typeof v === "number") v = [v]; | ||
1303 | if (v.length === 1 && v[0] <= 35) { | ||
1304 | return "0123456789abcdefghijklmnopqrstuvwxyz".charAt(v[0]); | ||
1305 | } | ||
1306 | return "<" + v + ">"; | ||
1307 | } | ||
1308 | function toBase(n, base) { | ||
1309 | base = bigInt(base); | ||
1310 | if (base.isZero()) { | ||
1311 | if (n.isZero()) return "0"; | ||
1312 | throw new Error("Cannot convert nonzero numbers to base 0."); | ||
1313 | } | ||
1314 | if (base.equals(-1)) { | ||
1315 | if (n.isZero()) return "0"; | ||
1316 | if (n.isNegative()) return new Array(1 - n).join("10"); | ||
1317 | return "1" + new Array(+n).join("01"); | ||
1318 | } | ||
1319 | var minusSign = ""; | ||
1320 | if (n.isNegative() && base.isPositive()) { | ||
1321 | minusSign = "-"; | ||
1322 | n = n.abs(); | ||
1323 | } | ||
1324 | if (base.equals(1)) { | ||
1325 | if (n.isZero()) return "0"; | ||
1326 | return minusSign + new Array(+n + 1).join(1); | ||
1327 | } | ||
1328 | var out = []; | ||
1329 | var left = n, divmod; | ||
1330 | while (left.isNegative() || left.compareAbs(base) >= 0) { | ||
1331 | divmod = left.divmod(base); | ||
1332 | left = divmod.quotient; | ||
1333 | var digit = divmod.remainder; | ||
1334 | if (digit.isNegative()) { | ||
1335 | digit = base.minus(digit).abs(); | ||
1336 | left = left.next(); | ||
1337 | } | ||
1338 | out.push(stringify(digit)); | ||
1339 | } | ||
1340 | out.push(stringify(left)); | ||
1341 | return minusSign + out.reverse().join(""); | ||
1342 | } | ||
1343 | |||
1344 | BigInteger.prototype.toString = function (radix) { | ||
1345 | if (radix === undefined) radix = 10; | ||
1346 | if (radix !== 10) return toBase(this, radix); | ||
1347 | var v = this.value, l = v.length, str = String(v[--l]), zeros = "0000000", digit; | ||
1348 | while (--l >= 0) { | ||
1349 | digit = String(v[l]); | ||
1350 | str += zeros.slice(digit.length) + digit; | ||
1351 | } | ||
1352 | var sign = this.sign ? "-" : ""; | ||
1353 | return sign + str; | ||
1354 | }; | ||
1355 | |||
1356 | SmallInteger.prototype.toString = function (radix) { | ||
1357 | if (radix === undefined) radix = 10; | ||
1358 | if (radix != 10) return toBase(this, radix); | ||
1359 | return String(this.value); | ||
1360 | }; | ||
1361 | BigInteger.prototype.toJSON = SmallInteger.prototype.toJSON = function() { return this.toString(); } | ||
1362 | |||
1363 | BigInteger.prototype.valueOf = function () { | ||
1364 | return +this.toString(); | ||
1365 | }; | ||
1366 | BigInteger.prototype.toJSNumber = BigInteger.prototype.valueOf; | ||
1367 | |||
1368 | SmallInteger.prototype.valueOf = function () { | ||
1369 | return this.value; | ||
1370 | }; | ||
1371 | SmallInteger.prototype.toJSNumber = SmallInteger.prototype.valueOf; | ||
1372 | |||
1373 | function parseStringValue(v) { | ||
1374 | if (isPrecise(+v)) { | ||
1375 | var x = +v; | ||
1376 | if (x === truncate(x)) | ||
1377 | return new SmallInteger(x); | ||
1378 | throw "Invalid integer: " + v; | ||
1379 | } | ||
1380 | var sign = v[0] === "-"; | ||
1381 | if (sign) v = v.slice(1); | ||
1382 | var split = v.split(/e/i); | ||
1383 | if (split.length > 2) throw new Error("Invalid integer: " + split.join("e")); | ||
1384 | if (split.length === 2) { | ||
1385 | var exp = split[1]; | ||
1386 | if (exp[0] === "+") exp = exp.slice(1); | ||
1387 | exp = +exp; | ||
1388 | if (exp !== truncate(exp) || !isPrecise(exp)) throw new Error("Invalid integer: " + exp + " is not a valid exponent."); | ||
1389 | var text = split[0]; | ||
1390 | var decimalPlace = text.indexOf("."); | ||
1391 | if (decimalPlace >= 0) { | ||
1392 | exp -= text.length - decimalPlace - 1; | ||
1393 | text = text.slice(0, decimalPlace) + text.slice(decimalPlace + 1); | ||
1394 | } | ||
1395 | if (exp < 0) throw new Error("Cannot include negative exponent part for integers"); | ||
1396 | text += (new Array(exp + 1)).join("0"); | ||
1397 | v = text; | ||
1398 | } | ||
1399 | var isValid = /^([0-9][0-9]*)$/.test(v); | ||
1400 | if (!isValid) throw new Error("Invalid integer: " + v); | ||
1401 | var r = [], max = v.length, l = LOG_BASE, min = max - l; | ||
1402 | while (max > 0) { | ||
1403 | r.push(+v.slice(min, max)); | ||
1404 | min -= l; | ||
1405 | if (min < 0) min = 0; | ||
1406 | max -= l; | ||
1407 | } | ||
1408 | trim(r); | ||
1409 | return new BigInteger(r, sign); | ||
1410 | } | ||
1411 | |||
1412 | function parseNumberValue(v) { | ||
1413 | if (isPrecise(v)) { | ||
1414 | if (v !== truncate(v)) throw new Error(v + " is not an integer."); | ||
1415 | return new SmallInteger(v); | ||
1416 | } | ||
1417 | return parseStringValue(v.toString()); | ||
1418 | } | ||
1419 | |||
1420 | function parseValue(v) { | ||
1421 | if (typeof v === "number") { | ||
1422 | return parseNumberValue(v); | ||
1423 | } | ||
1424 | if (typeof v === "string") { | ||
1425 | return parseStringValue(v); | ||
1426 | } | ||
1427 | return v; | ||
1428 | } | ||
1429 | // Pre-define numbers in range [-999,999] | ||
1430 | for (var i = 0; i < 1000; i++) { | ||
1431 | Integer[i] = new SmallInteger(i); | ||
1432 | if (i > 0) Integer[-i] = new SmallInteger(-i); | ||
1433 | } | ||
1434 | // Backwards compatibility | ||
1435 | Integer.one = Integer[1]; | ||
1436 | Integer.zero = Integer[0]; | ||
1437 | Integer.minusOne = Integer[-1]; | ||
1438 | Integer.max = max; | ||
1439 | Integer.min = min; | ||
1440 | Integer.gcd = gcd; | ||
1441 | Integer.lcm = lcm; | ||
1442 | Integer.isInstance = function (x) { return x instanceof BigInteger || x instanceof SmallInteger; }; | ||
1443 | Integer.randBetween = randBetween; | ||
1444 | |||
1445 | Integer.fromArray = function (digits, base, isNegative) { | ||
1446 | return parseBaseFromArray(digits.map(parseValue), parseValue(base || 10), isNegative); | ||
1447 | }; | ||
1448 | |||
1449 | return Integer; | ||
1450 | })(); | ||
1451 | |||
1452 | // Node.js check | ||
1453 | if (typeof module !== "undefined" && module.hasOwnProperty("exports")) { | ||
1454 | module.exports = bigInt; | ||
1455 | } | ||
1456 | |||
1457 | //amd check | ||
1458 | if ( typeof define === "function" && define.amd ) { | ||
1459 | define( "big-integer", [], function() { | ||
1460 | return bigInt; | ||
1461 | }); | ||
1462 | } | ||
1463 | |||
1464 | },{}],4:[function(require,module,exports){ | ||
1465 | |||
1466 | },{}],5:[function(require,module,exports){ | ||
1467 | var basex = require('base-x') | ||
1468 | var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | ||
1469 | |||
1470 | module.exports = basex(ALPHABET) | ||
1471 | |||
1472 | },{"base-x":1}],6:[function(require,module,exports){ | ||
1473 | 'use strict' | ||
1474 | |||
1475 | var base58 = require('bs58') | ||
1476 | var Buffer = require('safe-buffer').Buffer | ||
1477 | |||
1478 | module.exports = function (checksumFn) { | ||
1479 | // Encode a buffer as a base58-check encoded string | ||
1480 | function encode (payload) { | ||
1481 | var checksum = checksumFn(payload) | ||
1482 | |||
1483 | return base58.encode(Buffer.concat([ | ||
1484 | payload, | ||
1485 | checksum | ||
1486 | ], payload.length + 4)) | ||
1487 | } | ||
1488 | |||
1489 | function decodeRaw (buffer) { | ||
1490 | var payload = buffer.slice(0, -4) | ||
1491 | var checksum = buffer.slice(-4) | ||
1492 | var newChecksum = checksumFn(payload) | ||
1493 | |||
1494 | if (checksum[0] ^ newChecksum[0] | | ||
1495 | checksum[1] ^ newChecksum[1] | | ||
1496 | checksum[2] ^ newChecksum[2] | | ||
1497 | checksum[3] ^ newChecksum[3]) return | ||
1498 | |||
1499 | return payload | ||
1500 | } | ||
1501 | |||
1502 | // Decode a base58-check encoded string to a buffer, no result if checksum is wrong | ||
1503 | function decodeUnsafe (string) { | ||
1504 | var buffer = base58.decodeUnsafe(string) | ||
1505 | if (!buffer) return | ||
1506 | |||
1507 | return decodeRaw(buffer) | ||
1508 | } | ||
1509 | |||
1510 | function decode (string) { | ||
1511 | var buffer = base58.decode(string) | ||
1512 | var payload = decodeRaw(buffer, checksumFn) | ||
1513 | if (!payload) throw new Error('Invalid checksum') | ||
1514 | return payload | ||
1515 | } | ||
1516 | |||
1517 | return { | ||
1518 | encode: encode, | ||
1519 | decode: decode, | ||
1520 | decodeUnsafe: decodeUnsafe | ||
1521 | } | ||
1522 | } | ||
1523 | |||
1524 | },{"bs58":5,"safe-buffer":40}],7:[function(require,module,exports){ | ||
1525 | 'use strict' | ||
1526 | |||
1527 | var createHash = require('create-hash') | ||
1528 | var bs58checkBase = require('./base') | ||
1529 | |||
1530 | // SHA256(SHA256(buffer)) | ||
1531 | function sha256x2 (buffer) { | ||
1532 | var tmp = createHash('sha256').update(buffer).digest() | ||
1533 | return createHash('sha256').update(tmp).digest() | ||
1534 | } | ||
1535 | |||
1536 | module.exports = bs58checkBase(sha256x2) | ||
1537 | |||
1538 | },{"./base":6,"create-hash":15}],8:[function(require,module,exports){ | ||
1539 | /*! | ||
1540 | * The buffer module from node.js, for the browser. | ||
1541 | * | ||
1542 | * @author Feross Aboukhadijeh <https://feross.org> | ||
1543 | * @license MIT | ||
1544 | */ | ||
1545 | /* eslint-disable no-proto */ | ||
1546 | |||
1547 | 'use strict' | ||
1548 | |||
1549 | var base64 = require('base64-js') | ||
1550 | var ieee754 = require('ieee754') | ||
1551 | |||
1552 | exports.Buffer = Buffer | ||
1553 | exports.SlowBuffer = SlowBuffer | ||
1554 | exports.INSPECT_MAX_BYTES = 50 | ||
1555 | |||
1556 | var K_MAX_LENGTH = 0x7fffffff | ||
1557 | exports.kMaxLength = K_MAX_LENGTH | ||
1558 | |||
1559 | /** | ||
1560 | * If `Buffer.TYPED_ARRAY_SUPPORT`: | ||
1561 | * === true Use Uint8Array implementation (fastest) | ||
1562 | * === false Print warning and recommend using `buffer` v4.x which has an Object | ||
1563 | * implementation (most compatible, even IE6) | ||
1564 | * | ||
1565 | * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, | ||
1566 | * Opera 11.6+, iOS 4.2+. | ||
1567 | * | ||
1568 | * We report that the browser does not support typed arrays if the are not subclassable | ||
1569 | * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array` | ||
1570 | * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support | ||
1571 | * for __proto__ and has a buggy typed array implementation. | ||
1572 | */ | ||
1573 | Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport() | ||
1574 | |||
1575 | if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' && | ||
1576 | typeof console.error === 'function') { | ||
1577 | console.error( | ||
1578 | 'This browser lacks typed array (Uint8Array) support which is required by ' + | ||
1579 | '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.' | ||
1580 | ) | ||
1581 | } | ||
1582 | |||
1583 | function typedArraySupport () { | ||
1584 | // Can typed array instances can be augmented? | ||
1585 | try { | ||
1586 | var arr = new Uint8Array(1) | ||
1587 | arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }} | ||
1588 | return arr.foo() === 42 | ||
1589 | } catch (e) { | ||
1590 | return false | ||
1591 | } | ||
1592 | } | ||
1593 | |||
1594 | function createBuffer (length) { | ||
1595 | if (length > K_MAX_LENGTH) { | ||
1596 | throw new RangeError('Invalid typed array length') | ||
1597 | } | ||
1598 | // Return an augmented `Uint8Array` instance | ||
1599 | var buf = new Uint8Array(length) | ||
1600 | buf.__proto__ = Buffer.prototype | ||
1601 | return buf | ||
1602 | } | ||
1603 | |||
1604 | /** | ||
1605 | * The Buffer constructor returns instances of `Uint8Array` that have their | ||
1606 | * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of | ||
1607 | * `Uint8Array`, so the returned instances will have all the node `Buffer` methods | ||
1608 | * and the `Uint8Array` methods. Square bracket notation works as expected -- it | ||
1609 | * returns a single octet. | ||
1610 | * | ||
1611 | * The `Uint8Array` prototype remains unmodified. | ||
1612 | */ | ||
1613 | |||
1614 | function Buffer (arg, encodingOrOffset, length) { | ||
1615 | // Common case. | ||
1616 | if (typeof arg === 'number') { | ||
1617 | if (typeof encodingOrOffset === 'string') { | ||
1618 | throw new Error( | ||
1619 | 'If encoding is specified then the first argument must be a string' | ||
1620 | ) | ||
1621 | } | ||
1622 | return allocUnsafe(arg) | ||
1623 | } | ||
1624 | return from(arg, encodingOrOffset, length) | ||
1625 | } | ||
1626 | |||
1627 | // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 | ||
1628 | if (typeof Symbol !== 'undefined' && Symbol.species && | ||
1629 | Buffer[Symbol.species] === Buffer) { | ||
1630 | Object.defineProperty(Buffer, Symbol.species, { | ||
1631 | value: null, | ||
1632 | configurable: true, | ||
1633 | enumerable: false, | ||
1634 | writable: false | ||
1635 | }) | ||
1636 | } | ||
1637 | |||
1638 | Buffer.poolSize = 8192 // not used by this implementation | ||
1639 | |||
1640 | function from (value, encodingOrOffset, length) { | ||
1641 | if (typeof value === 'number') { | ||
1642 | throw new TypeError('"value" argument must not be a number') | ||
1643 | } | ||
1644 | |||
1645 | if (isArrayBuffer(value)) { | ||
1646 | return fromArrayBuffer(value, encodingOrOffset, length) | ||
1647 | } | ||
1648 | |||
1649 | if (typeof value === 'string') { | ||
1650 | return fromString(value, encodingOrOffset) | ||
1651 | } | ||
1652 | |||
1653 | return fromObject(value) | ||
1654 | } | ||
1655 | |||
1656 | /** | ||
1657 | * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError | ||
1658 | * if value is a number. | ||
1659 | * Buffer.from(str[, encoding]) | ||
1660 | * Buffer.from(array) | ||
1661 | * Buffer.from(buffer) | ||
1662 | * Buffer.from(arrayBuffer[, byteOffset[, length]]) | ||
1663 | **/ | ||
1664 | Buffer.from = function (value, encodingOrOffset, length) { | ||
1665 | return from(value, encodingOrOffset, length) | ||
1666 | } | ||
1667 | |||
1668 | // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug: | ||
1669 | // https://github.com/feross/buffer/pull/148 | ||
1670 | Buffer.prototype.__proto__ = Uint8Array.prototype | ||
1671 | Buffer.__proto__ = Uint8Array | ||
1672 | |||
1673 | function assertSize (size) { | ||
1674 | if (typeof size !== 'number') { | ||
1675 | throw new TypeError('"size" argument must be a number') | ||
1676 | } else if (size < 0) { | ||
1677 | throw new RangeError('"size" argument must not be negative') | ||
1678 | } | ||
1679 | } | ||
1680 | |||
1681 | function alloc (size, fill, encoding) { | ||
1682 | assertSize(size) | ||
1683 | if (size <= 0) { | ||
1684 | return createBuffer(size) | ||
1685 | } | ||
1686 | if (fill !== undefined) { | ||
1687 | // Only pay attention to encoding if it's a string. This | ||
1688 | // prevents accidentally sending in a number that would | ||
1689 | // be interpretted as a start offset. | ||
1690 | return typeof encoding === 'string' | ||
1691 | ? createBuffer(size).fill(fill, encoding) | ||
1692 | : createBuffer(size).fill(fill) | ||
1693 | } | ||
1694 | return createBuffer(size) | ||
1695 | } | ||
1696 | |||
1697 | /** | ||
1698 | * Creates a new filled Buffer instance. | ||
1699 | * alloc(size[, fill[, encoding]]) | ||
1700 | **/ | ||
1701 | Buffer.alloc = function (size, fill, encoding) { | ||
1702 | return alloc(size, fill, encoding) | ||
1703 | } | ||
1704 | |||
1705 | function allocUnsafe (size) { | ||
1706 | assertSize(size) | ||
1707 | return createBuffer(size < 0 ? 0 : checked(size) | 0) | ||
1708 | } | ||
1709 | |||
1710 | /** | ||
1711 | * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. | ||
1712 | * */ | ||
1713 | Buffer.allocUnsafe = function (size) { | ||
1714 | return allocUnsafe(size) | ||
1715 | } | ||
1716 | /** | ||
1717 | * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. | ||
1718 | */ | ||
1719 | Buffer.allocUnsafeSlow = function (size) { | ||
1720 | return allocUnsafe(size) | ||
1721 | } | ||
1722 | |||
1723 | function fromString (string, encoding) { | ||
1724 | if (typeof encoding !== 'string' || encoding === '') { | ||
1725 | encoding = 'utf8' | ||
1726 | } | ||
1727 | |||
1728 | if (!Buffer.isEncoding(encoding)) { | ||
1729 | throw new TypeError('"encoding" must be a valid string encoding') | ||
1730 | } | ||
1731 | |||
1732 | var length = byteLength(string, encoding) | 0 | ||
1733 | var buf = createBuffer(length) | ||
1734 | |||
1735 | var actual = buf.write(string, encoding) | ||
1736 | |||
1737 | if (actual !== length) { | ||
1738 | // Writing a hex string, for example, that contains invalid characters will | ||
1739 | // cause everything after the first invalid character to be ignored. (e.g. | ||
1740 | // 'abxxcd' will be treated as 'ab') | ||
1741 | buf = buf.slice(0, actual) | ||
1742 | } | ||
1743 | |||
1744 | return buf | ||
1745 | } | ||
1746 | |||
1747 | function fromArrayLike (array) { | ||
1748 | var length = array.length < 0 ? 0 : checked(array.length) | 0 | ||
1749 | var buf = createBuffer(length) | ||
1750 | for (var i = 0; i < length; i += 1) { | ||
1751 | buf[i] = array[i] & 255 | ||
1752 | } | ||
1753 | return buf | ||
1754 | } | ||
1755 | |||
1756 | function fromArrayBuffer (array, byteOffset, length) { | ||
1757 | if (byteOffset < 0 || array.byteLength < byteOffset) { | ||
1758 | throw new RangeError('\'offset\' is out of bounds') | ||
1759 | } | ||
1760 | |||
1761 | if (array.byteLength < byteOffset + (length || 0)) { | ||
1762 | throw new RangeError('\'length\' is out of bounds') | ||
1763 | } | ||
1764 | |||
1765 | var buf | ||
1766 | if (byteOffset === undefined && length === undefined) { | ||
1767 | buf = new Uint8Array(array) | ||
1768 | } else if (length === undefined) { | ||
1769 | buf = new Uint8Array(array, byteOffset) | ||
1770 | } else { | ||
1771 | buf = new Uint8Array(array, byteOffset, length) | ||
1772 | } | ||
1773 | |||
1774 | // Return an augmented `Uint8Array` instance | ||
1775 | buf.__proto__ = Buffer.prototype | ||
1776 | return buf | ||
1777 | } | ||
1778 | |||
1779 | function fromObject (obj) { | ||
1780 | if (Buffer.isBuffer(obj)) { | ||
1781 | var len = checked(obj.length) | 0 | ||
1782 | var buf = createBuffer(len) | ||
1783 | |||
1784 | if (buf.length === 0) { | ||
1785 | return buf | ||
1786 | } | ||
1787 | |||
1788 | obj.copy(buf, 0, 0, len) | ||
1789 | return buf | ||
1790 | } | ||
1791 | |||
1792 | if (obj) { | ||
1793 | if (isArrayBufferView(obj) || 'length' in obj) { | ||
1794 | if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) { | ||
1795 | return createBuffer(0) | ||
1796 | } | ||
1797 | return fromArrayLike(obj) | ||
1798 | } | ||
1799 | |||
1800 | if (obj.type === 'Buffer' && Array.isArray(obj.data)) { | ||
1801 | return fromArrayLike(obj.data) | ||
1802 | } | ||
1803 | } | ||
1804 | |||
1805 | throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.') | ||
1806 | } | ||
1807 | |||
1808 | function checked (length) { | ||
1809 | // Note: cannot use `length < K_MAX_LENGTH` here because that fails when | ||
1810 | // length is NaN (which is otherwise coerced to zero.) | ||
1811 | if (length >= K_MAX_LENGTH) { | ||
1812 | throw new RangeError('Attempt to allocate Buffer larger than maximum ' + | ||
1813 | 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes') | ||
1814 | } | ||
1815 | return length | 0 | ||
1816 | } | ||
1817 | |||
1818 | function SlowBuffer (length) { | ||
1819 | if (+length != length) { // eslint-disable-line eqeqeq | ||
1820 | length = 0 | ||
1821 | } | ||
1822 | return Buffer.alloc(+length) | ||
1823 | } | ||
1824 | |||
1825 | Buffer.isBuffer = function isBuffer (b) { | ||
1826 | return b != null && b._isBuffer === true | ||
1827 | } | ||
1828 | |||
1829 | Buffer.compare = function compare (a, b) { | ||
1830 | if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { | ||
1831 | throw new TypeError('Arguments must be Buffers') | ||
1832 | } | ||
1833 | |||
1834 | if (a === b) return 0 | ||
1835 | |||
1836 | var x = a.length | ||
1837 | var y = b.length | ||
1838 | |||
1839 | for (var i = 0, len = Math.min(x, y); i < len; ++i) { | ||
1840 | if (a[i] !== b[i]) { | ||
1841 | x = a[i] | ||
1842 | y = b[i] | ||
1843 | break | ||
1844 | } | ||
1845 | } | ||
1846 | |||
1847 | if (x < y) return -1 | ||
1848 | if (y < x) return 1 | ||
1849 | return 0 | ||
1850 | } | ||
1851 | |||
1852 | Buffer.isEncoding = function isEncoding (encoding) { | ||
1853 | switch (String(encoding).toLowerCase()) { | ||
1854 | case 'hex': | ||
1855 | case 'utf8': | ||
1856 | case 'utf-8': | ||
1857 | case 'ascii': | ||
1858 | case 'latin1': | ||
1859 | case 'binary': | ||
1860 | case 'base64': | ||
1861 | case 'ucs2': | ||
1862 | case 'ucs-2': | ||
1863 | case 'utf16le': | ||
1864 | case 'utf-16le': | ||
1865 | return true | ||
1866 | default: | ||
1867 | return false | ||
1868 | } | ||
1869 | } | ||
1870 | |||
1871 | Buffer.concat = function concat (list, length) { | ||
1872 | if (!Array.isArray(list)) { | ||
1873 | throw new TypeError('"list" argument must be an Array of Buffers') | ||
1874 | } | ||
1875 | |||
1876 | if (list.length === 0) { | ||
1877 | return Buffer.alloc(0) | ||
1878 | } | ||
1879 | |||
1880 | var i | ||
1881 | if (length === undefined) { | ||
1882 | length = 0 | ||
1883 | for (i = 0; i < list.length; ++i) { | ||
1884 | length += list[i].length | ||
1885 | } | ||
1886 | } | ||
1887 | |||
1888 | var buffer = Buffer.allocUnsafe(length) | ||
1889 | var pos = 0 | ||
1890 | for (i = 0; i < list.length; ++i) { | ||
1891 | var buf = list[i] | ||
1892 | if (!Buffer.isBuffer(buf)) { | ||
1893 | throw new TypeError('"list" argument must be an Array of Buffers') | ||
1894 | } | ||
1895 | buf.copy(buffer, pos) | ||
1896 | pos += buf.length | ||
1897 | } | ||
1898 | return buffer | ||
1899 | } | ||
1900 | |||
1901 | function byteLength (string, encoding) { | ||
1902 | if (Buffer.isBuffer(string)) { | ||
1903 | return string.length | ||
1904 | } | ||
1905 | if (isArrayBufferView(string) || isArrayBuffer(string)) { | ||
1906 | return string.byteLength | ||
1907 | } | ||
1908 | if (typeof string !== 'string') { | ||
1909 | string = '' + string | ||
1910 | } | ||
1911 | |||
1912 | var len = string.length | ||
1913 | if (len === 0) return 0 | ||
1914 | |||
1915 | // Use a for loop to avoid recursion | ||
1916 | var loweredCase = false | ||
1917 | for (;;) { | ||
1918 | switch (encoding) { | ||
1919 | case 'ascii': | ||
1920 | case 'latin1': | ||
1921 | case 'binary': | ||
1922 | return len | ||
1923 | case 'utf8': | ||
1924 | case 'utf-8': | ||
1925 | case undefined: | ||
1926 | return utf8ToBytes(string).length | ||
1927 | case 'ucs2': | ||
1928 | case 'ucs-2': | ||
1929 | case 'utf16le': | ||
1930 | case 'utf-16le': | ||
1931 | return len * 2 | ||
1932 | case 'hex': | ||
1933 | return len >>> 1 | ||
1934 | case 'base64': | ||
1935 | return base64ToBytes(string).length | ||
1936 | default: | ||
1937 | if (loweredCase) return utf8ToBytes(string).length // assume utf8 | ||
1938 | encoding = ('' + encoding).toLowerCase() | ||
1939 | loweredCase = true | ||
1940 | } | ||
1941 | } | ||
1942 | } | ||
1943 | Buffer.byteLength = byteLength | ||
1944 | |||
1945 | function slowToString (encoding, start, end) { | ||
1946 | var loweredCase = false | ||
1947 | |||
1948 | // No need to verify that "this.length <= MAX_UINT32" since it's a read-only | ||
1949 | // property of a typed array. | ||
1950 | |||
1951 | // This behaves neither like String nor Uint8Array in that we set start/end | ||
1952 | // to their upper/lower bounds if the value passed is out of range. | ||
1953 | // undefined is handled specially as per ECMA-262 6th Edition, | ||
1954 | // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. | ||
1955 | if (start === undefined || start < 0) { | ||
1956 | start = 0 | ||
1957 | } | ||
1958 | // Return early if start > this.length. Done here to prevent potential uint32 | ||
1959 | // coercion fail below. | ||
1960 | if (start > this.length) { | ||
1961 | return '' | ||
1962 | } | ||
1963 | |||
1964 | if (end === undefined || end > this.length) { | ||
1965 | end = this.length | ||
1966 | } | ||
1967 | |||
1968 | if (end <= 0) { | ||
1969 | return '' | ||
1970 | } | ||
1971 | |||
1972 | // Force coersion to uint32. This will also coerce falsey/NaN values to 0. | ||
1973 | end >>>= 0 | ||
1974 | start >>>= 0 | ||
1975 | |||
1976 | if (end <= start) { | ||
1977 | return '' | ||
1978 | } | ||
1979 | |||
1980 | if (!encoding) encoding = 'utf8' | ||
1981 | |||
1982 | while (true) { | ||
1983 | switch (encoding) { | ||
1984 | case 'hex': | ||
1985 | return hexSlice(this, start, end) | ||
1986 | |||
1987 | case 'utf8': | ||
1988 | case 'utf-8': | ||
1989 | return utf8Slice(this, start, end) | ||
1990 | |||
1991 | case 'ascii': | ||
1992 | return asciiSlice(this, start, end) | ||
1993 | |||
1994 | case 'latin1': | ||
1995 | case 'binary': | ||
1996 | return latin1Slice(this, start, end) | ||
1997 | |||
1998 | case 'base64': | ||
1999 | return base64Slice(this, start, end) | ||
2000 | |||
2001 | case 'ucs2': | ||
2002 | case 'ucs-2': | ||
2003 | case 'utf16le': | ||
2004 | case 'utf-16le': | ||
2005 | return utf16leSlice(this, start, end) | ||
2006 | |||
2007 | default: | ||
2008 | if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) | ||
2009 | encoding = (encoding + '').toLowerCase() | ||
2010 | loweredCase = true | ||
2011 | } | ||
2012 | } | ||
2013 | } | ||
2014 | |||
2015 | // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package) | ||
2016 | // to detect a Buffer instance. It's not possible to use `instanceof Buffer` | ||
2017 | // reliably in a browserify context because there could be multiple different | ||
2018 | // copies of the 'buffer' package in use. This method works even for Buffer | ||
2019 | // instances that were created from another copy of the `buffer` package. | ||
2020 | // See: https://github.com/feross/buffer/issues/154 | ||
2021 | Buffer.prototype._isBuffer = true | ||
2022 | |||
2023 | function swap (b, n, m) { | ||
2024 | var i = b[n] | ||
2025 | b[n] = b[m] | ||
2026 | b[m] = i | ||
2027 | } | ||
2028 | |||
2029 | Buffer.prototype.swap16 = function swap16 () { | ||
2030 | var len = this.length | ||
2031 | if (len % 2 !== 0) { | ||
2032 | throw new RangeError('Buffer size must be a multiple of 16-bits') | ||
2033 | } | ||
2034 | for (var i = 0; i < len; i += 2) { | ||
2035 | swap(this, i, i + 1) | ||
2036 | } | ||
2037 | return this | ||
2038 | } | ||
2039 | |||
2040 | Buffer.prototype.swap32 = function swap32 () { | ||
2041 | var len = this.length | ||
2042 | if (len % 4 !== 0) { | ||
2043 | throw new RangeError('Buffer size must be a multiple of 32-bits') | ||
2044 | } | ||
2045 | for (var i = 0; i < len; i += 4) { | ||
2046 | swap(this, i, i + 3) | ||
2047 | swap(this, i + 1, i + 2) | ||
2048 | } | ||
2049 | return this | ||
2050 | } | ||
2051 | |||
2052 | Buffer.prototype.swap64 = function swap64 () { | ||
2053 | var len = this.length | ||
2054 | if (len % 8 !== 0) { | ||
2055 | throw new RangeError('Buffer size must be a multiple of 64-bits') | ||
2056 | } | ||
2057 | for (var i = 0; i < len; i += 8) { | ||
2058 | swap(this, i, i + 7) | ||
2059 | swap(this, i + 1, i + 6) | ||
2060 | swap(this, i + 2, i + 5) | ||
2061 | swap(this, i + 3, i + 4) | ||
2062 | } | ||
2063 | return this | ||
2064 | } | ||
2065 | |||
2066 | Buffer.prototype.toString = function toString () { | ||
2067 | var length = this.length | ||
2068 | if (length === 0) return '' | ||
2069 | if (arguments.length === 0) return utf8Slice(this, 0, length) | ||
2070 | return slowToString.apply(this, arguments) | ||
2071 | } | ||
2072 | |||
2073 | Buffer.prototype.equals = function equals (b) { | ||
2074 | if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') | ||
2075 | if (this === b) return true | ||
2076 | return Buffer.compare(this, b) === 0 | ||
2077 | } | ||
2078 | |||
2079 | Buffer.prototype.inspect = function inspect () { | ||
2080 | var str = '' | ||
2081 | var max = exports.INSPECT_MAX_BYTES | ||
2082 | if (this.length > 0) { | ||
2083 | str = this.toString('hex', 0, max).match(/.{2}/g).join(' ') | ||
2084 | if (this.length > max) str += ' ... ' | ||
2085 | } | ||
2086 | return '<Buffer ' + str + '>' | ||
2087 | } | ||
2088 | |||
2089 | Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { | ||
2090 | if (!Buffer.isBuffer(target)) { | ||
2091 | throw new TypeError('Argument must be a Buffer') | ||
2092 | } | ||
2093 | |||
2094 | if (start === undefined) { | ||
2095 | start = 0 | ||
2096 | } | ||
2097 | if (end === undefined) { | ||
2098 | end = target ? target.length : 0 | ||
2099 | } | ||
2100 | if (thisStart === undefined) { | ||
2101 | thisStart = 0 | ||
2102 | } | ||
2103 | if (thisEnd === undefined) { | ||
2104 | thisEnd = this.length | ||
2105 | } | ||
2106 | |||
2107 | if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) { | ||
2108 | throw new RangeError('out of range index') | ||
2109 | } | ||
2110 | |||
2111 | if (thisStart >= thisEnd && start >= end) { | ||
2112 | return 0 | ||
2113 | } | ||
2114 | if (thisStart >= thisEnd) { | ||
2115 | return -1 | ||
2116 | } | ||
2117 | if (start >= end) { | ||
2118 | return 1 | ||
2119 | } | ||
2120 | |||
2121 | start >>>= 0 | ||
2122 | end >>>= 0 | ||
2123 | thisStart >>>= 0 | ||
2124 | thisEnd >>>= 0 | ||
2125 | |||
2126 | if (this === target) return 0 | ||
2127 | |||
2128 | var x = thisEnd - thisStart | ||
2129 | var y = end - start | ||
2130 | var len = Math.min(x, y) | ||
2131 | |||
2132 | var thisCopy = this.slice(thisStart, thisEnd) | ||
2133 | var targetCopy = target.slice(start, end) | ||
2134 | |||
2135 | for (var i = 0; i < len; ++i) { | ||
2136 | if (thisCopy[i] !== targetCopy[i]) { | ||
2137 | x = thisCopy[i] | ||
2138 | y = targetCopy[i] | ||
2139 | break | ||
2140 | } | ||
2141 | } | ||
2142 | |||
2143 | if (x < y) return -1 | ||
2144 | if (y < x) return 1 | ||
2145 | return 0 | ||
2146 | } | ||
2147 | |||
2148 | // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`, | ||
2149 | // OR the last index of `val` in `buffer` at offset <= `byteOffset`. | ||
2150 | // | ||
2151 | // Arguments: | ||
2152 | // - buffer - a Buffer to search | ||
2153 | // - val - a string, Buffer, or number | ||
2154 | // - byteOffset - an index into `buffer`; will be clamped to an int32 | ||
2155 | // - encoding - an optional encoding, relevant is val is a string | ||
2156 | // - dir - true for indexOf, false for lastIndexOf | ||
2157 | function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { | ||
2158 | // Empty buffer means no match | ||
2159 | if (buffer.length === 0) return -1 | ||
2160 | |||
2161 | // Normalize byteOffset | ||
2162 | if (typeof byteOffset === 'string') { | ||
2163 | encoding = byteOffset | ||
2164 | byteOffset = 0 | ||
2165 | } else if (byteOffset > 0x7fffffff) { | ||
2166 | byteOffset = 0x7fffffff | ||
2167 | } else if (byteOffset < -0x80000000) { | ||
2168 | byteOffset = -0x80000000 | ||
2169 | } | ||
2170 | byteOffset = +byteOffset // Coerce to Number. | ||
2171 | if (numberIsNaN(byteOffset)) { | ||
2172 | // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer | ||
2173 | byteOffset = dir ? 0 : (buffer.length - 1) | ||
2174 | } | ||
2175 | |||
2176 | // Normalize byteOffset: negative offsets start from the end of the buffer | ||
2177 | if (byteOffset < 0) byteOffset = buffer.length + byteOffset | ||
2178 | if (byteOffset >= buffer.length) { | ||
2179 | if (dir) return -1 | ||
2180 | else byteOffset = buffer.length - 1 | ||
2181 | } else if (byteOffset < 0) { | ||
2182 | if (dir) byteOffset = 0 | ||
2183 | else return -1 | ||
2184 | } | ||
2185 | |||
2186 | // Normalize val | ||
2187 | if (typeof val === 'string') { | ||
2188 | val = Buffer.from(val, encoding) | ||
2189 | } | ||
2190 | |||
2191 | // Finally, search either indexOf (if dir is true) or lastIndexOf | ||
2192 | if (Buffer.isBuffer(val)) { | ||
2193 | // Special case: looking for empty string/buffer always fails | ||
2194 | if (val.length === 0) { | ||
2195 | return -1 | ||
2196 | } | ||
2197 | return arrayIndexOf(buffer, val, byteOffset, encoding, dir) | ||
2198 | } else if (typeof val === 'number') { | ||
2199 | val = val & 0xFF // Search for a byte value [0-255] | ||
2200 | if (typeof Uint8Array.prototype.indexOf === 'function') { | ||
2201 | if (dir) { | ||
2202 | return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset) | ||
2203 | } else { | ||
2204 | return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset) | ||
2205 | } | ||
2206 | } | ||
2207 | return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir) | ||
2208 | } | ||
2209 | |||
2210 | throw new TypeError('val must be string, number or Buffer') | ||
2211 | } | ||
2212 | |||
2213 | function arrayIndexOf (arr, val, byteOffset, encoding, dir) { | ||
2214 | var indexSize = 1 | ||
2215 | var arrLength = arr.length | ||
2216 | var valLength = val.length | ||
2217 | |||
2218 | if (encoding !== undefined) { | ||
2219 | encoding = String(encoding).toLowerCase() | ||
2220 | if (encoding === 'ucs2' || encoding === 'ucs-2' || | ||
2221 | encoding === 'utf16le' || encoding === 'utf-16le') { | ||
2222 | if (arr.length < 2 || val.length < 2) { | ||
2223 | return -1 | ||
2224 | } | ||
2225 | indexSize = 2 | ||
2226 | arrLength /= 2 | ||
2227 | valLength /= 2 | ||
2228 | byteOffset /= 2 | ||
2229 | } | ||
2230 | } | ||
2231 | |||
2232 | function read (buf, i) { | ||
2233 | if (indexSize === 1) { | ||
2234 | return buf[i] | ||
2235 | } else { | ||
2236 | return buf.readUInt16BE(i * indexSize) | ||
2237 | } | ||
2238 | } | ||
2239 | |||
2240 | var i | ||
2241 | if (dir) { | ||
2242 | var foundIndex = -1 | ||
2243 | for (i = byteOffset; i < arrLength; i++) { | ||
2244 | if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { | ||
2245 | if (foundIndex === -1) foundIndex = i | ||
2246 | if (i - foundIndex + 1 === valLength) return foundIndex * indexSize | ||
2247 | } else { | ||
2248 | if (foundIndex !== -1) i -= i - foundIndex | ||
2249 | foundIndex = -1 | ||
2250 | } | ||
2251 | } | ||
2252 | } else { | ||
2253 | if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength | ||
2254 | for (i = byteOffset; i >= 0; i--) { | ||
2255 | var found = true | ||
2256 | for (var j = 0; j < valLength; j++) { | ||
2257 | if (read(arr, i + j) !== read(val, j)) { | ||
2258 | found = false | ||
2259 | break | ||
2260 | } | ||
2261 | } | ||
2262 | if (found) return i | ||
2263 | } | ||
2264 | } | ||
2265 | |||
2266 | return -1 | ||
2267 | } | ||
2268 | |||
2269 | Buffer.prototype.includes = function includes (val, byteOffset, encoding) { | ||
2270 | return this.indexOf(val, byteOffset, encoding) !== -1 | ||
2271 | } | ||
2272 | |||
2273 | Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) { | ||
2274 | return bidirectionalIndexOf(this, val, byteOffset, encoding, true) | ||
2275 | } | ||
2276 | |||
2277 | Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) { | ||
2278 | return bidirectionalIndexOf(this, val, byteOffset, encoding, false) | ||
2279 | } | ||
2280 | |||
2281 | function hexWrite (buf, string, offset, length) { | ||
2282 | offset = Number(offset) || 0 | ||
2283 | var remaining = buf.length - offset | ||
2284 | if (!length) { | ||
2285 | length = remaining | ||
2286 | } else { | ||
2287 | length = Number(length) | ||
2288 | if (length > remaining) { | ||
2289 | length = remaining | ||
2290 | } | ||
2291 | } | ||
2292 | |||
2293 | // must be an even number of digits | ||
2294 | var strLen = string.length | ||
2295 | if (strLen % 2 !== 0) throw new TypeError('Invalid hex string') | ||
2296 | |||
2297 | if (length > strLen / 2) { | ||
2298 | length = strLen / 2 | ||
2299 | } | ||
2300 | for (var i = 0; i < length; ++i) { | ||
2301 | var parsed = parseInt(string.substr(i * 2, 2), 16) | ||
2302 | if (numberIsNaN(parsed)) return i | ||
2303 | buf[offset + i] = parsed | ||
2304 | } | ||
2305 | return i | ||
2306 | } | ||
2307 | |||
2308 | function utf8Write (buf, string, offset, length) { | ||
2309 | return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) | ||
2310 | } | ||
2311 | |||
2312 | function asciiWrite (buf, string, offset, length) { | ||
2313 | return blitBuffer(asciiToBytes(string), buf, offset, length) | ||
2314 | } | ||
2315 | |||
2316 | function latin1Write (buf, string, offset, length) { | ||
2317 | return asciiWrite(buf, string, offset, length) | ||
2318 | } | ||
2319 | |||
2320 | function base64Write (buf, string, offset, length) { | ||
2321 | return blitBuffer(base64ToBytes(string), buf, offset, length) | ||
2322 | } | ||
2323 | |||
2324 | function ucs2Write (buf, string, offset, length) { | ||
2325 | return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) | ||
2326 | } | ||
2327 | |||
2328 | Buffer.prototype.write = function write (string, offset, length, encoding) { | ||
2329 | // Buffer#write(string) | ||
2330 | if (offset === undefined) { | ||
2331 | encoding = 'utf8' | ||
2332 | length = this.length | ||
2333 | offset = 0 | ||
2334 | // Buffer#write(string, encoding) | ||
2335 | } else if (length === undefined && typeof offset === 'string') { | ||
2336 | encoding = offset | ||
2337 | length = this.length | ||
2338 | offset = 0 | ||
2339 | // Buffer#write(string, offset[, length][, encoding]) | ||
2340 | } else if (isFinite(offset)) { | ||
2341 | offset = offset >>> 0 | ||
2342 | if (isFinite(length)) { | ||
2343 | length = length >>> 0 | ||
2344 | if (encoding === undefined) encoding = 'utf8' | ||
2345 | } else { | ||
2346 | encoding = length | ||
2347 | length = undefined | ||
2348 | } | ||
2349 | } else { | ||
2350 | throw new Error( | ||
2351 | 'Buffer.write(string, encoding, offset[, length]) is no longer supported' | ||
2352 | ) | ||
2353 | } | ||
2354 | |||
2355 | var remaining = this.length - offset | ||
2356 | if (length === undefined || length > remaining) length = remaining | ||
2357 | |||
2358 | if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { | ||
2359 | throw new RangeError('Attempt to write outside buffer bounds') | ||
2360 | } | ||
2361 | |||
2362 | if (!encoding) encoding = 'utf8' | ||
2363 | |||
2364 | var loweredCase = false | ||
2365 | for (;;) { | ||
2366 | switch (encoding) { | ||
2367 | case 'hex': | ||
2368 | return hexWrite(this, string, offset, length) | ||
2369 | |||
2370 | case 'utf8': | ||
2371 | case 'utf-8': | ||
2372 | return utf8Write(this, string, offset, length) | ||
2373 | |||
2374 | case 'ascii': | ||
2375 | return asciiWrite(this, string, offset, length) | ||
2376 | |||
2377 | case 'latin1': | ||
2378 | case 'binary': | ||
2379 | return latin1Write(this, string, offset, length) | ||
2380 | |||
2381 | case 'base64': | ||
2382 | // Warning: maxLength not taken into account in base64Write | ||
2383 | return base64Write(this, string, offset, length) | ||
2384 | |||
2385 | case 'ucs2': | ||
2386 | case 'ucs-2': | ||
2387 | case 'utf16le': | ||
2388 | case 'utf-16le': | ||
2389 | return ucs2Write(this, string, offset, length) | ||
2390 | |||
2391 | default: | ||
2392 | if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) | ||
2393 | encoding = ('' + encoding).toLowerCase() | ||
2394 | loweredCase = true | ||
2395 | } | ||
2396 | } | ||
2397 | } | ||
2398 | |||
2399 | Buffer.prototype.toJSON = function toJSON () { | ||
2400 | return { | ||
2401 | type: 'Buffer', | ||
2402 | data: Array.prototype.slice.call(this._arr || this, 0) | ||
2403 | } | ||
2404 | } | ||
2405 | |||
2406 | function base64Slice (buf, start, end) { | ||
2407 | if (start === 0 && end === buf.length) { | ||
2408 | return base64.fromByteArray(buf) | ||
2409 | } else { | ||
2410 | return base64.fromByteArray(buf.slice(start, end)) | ||
2411 | } | ||
2412 | } | ||
2413 | |||
2414 | function utf8Slice (buf, start, end) { | ||
2415 | end = Math.min(buf.length, end) | ||
2416 | var res = [] | ||
2417 | |||
2418 | var i = start | ||
2419 | while (i < end) { | ||
2420 | var firstByte = buf[i] | ||
2421 | var codePoint = null | ||
2422 | var bytesPerSequence = (firstByte > 0xEF) ? 4 | ||
2423 | : (firstByte > 0xDF) ? 3 | ||
2424 | : (firstByte > 0xBF) ? 2 | ||
2425 | : 1 | ||
2426 | |||
2427 | if (i + bytesPerSequence <= end) { | ||
2428 | var secondByte, thirdByte, fourthByte, tempCodePoint | ||
2429 | |||
2430 | switch (bytesPerSequence) { | ||
2431 | case 1: | ||
2432 | if (firstByte < 0x80) { | ||
2433 | codePoint = firstByte | ||
2434 | } | ||
2435 | break | ||
2436 | case 2: | ||
2437 | secondByte = buf[i + 1] | ||
2438 | if ((secondByte & 0xC0) === 0x80) { | ||
2439 | tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F) | ||
2440 | if (tempCodePoint > 0x7F) { | ||
2441 | codePoint = tempCodePoint | ||
2442 | } | ||
2443 | } | ||
2444 | break | ||
2445 | case 3: | ||
2446 | secondByte = buf[i + 1] | ||
2447 | thirdByte = buf[i + 2] | ||
2448 | if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { | ||
2449 | tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F) | ||
2450 | if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { | ||
2451 | codePoint = tempCodePoint | ||
2452 | } | ||
2453 | } | ||
2454 | break | ||
2455 | case 4: | ||
2456 | secondByte = buf[i + 1] | ||
2457 | thirdByte = buf[i + 2] | ||
2458 | fourthByte = buf[i + 3] | ||
2459 | if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { | ||
2460 | tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F) | ||
2461 | if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { | ||
2462 | codePoint = tempCodePoint | ||
2463 | } | ||
2464 | } | ||
2465 | } | ||
2466 | } | ||
2467 | |||
2468 | if (codePoint === null) { | ||
2469 | // we did not generate a valid codePoint so insert a | ||
2470 | // replacement char (U+FFFD) and advance only 1 byte | ||
2471 | codePoint = 0xFFFD | ||
2472 | bytesPerSequence = 1 | ||
2473 | } else if (codePoint > 0xFFFF) { | ||
2474 | // encode to utf16 (surrogate pair dance) | ||
2475 | codePoint -= 0x10000 | ||
2476 | res.push(codePoint >>> 10 & 0x3FF | 0xD800) | ||
2477 | codePoint = 0xDC00 | codePoint & 0x3FF | ||
2478 | } | ||
2479 | |||
2480 | res.push(codePoint) | ||
2481 | i += bytesPerSequence | ||
2482 | } | ||
2483 | |||
2484 | return decodeCodePointsArray(res) | ||
2485 | } | ||
2486 | |||
2487 | // Based on http://stackoverflow.com/a/22747272/680742, the browser with | ||
2488 | // the lowest limit is Chrome, with 0x10000 args. | ||
2489 | // We go 1 magnitude less, for safety | ||
2490 | var MAX_ARGUMENTS_LENGTH = 0x1000 | ||
2491 | |||
2492 | function decodeCodePointsArray (codePoints) { | ||
2493 | var len = codePoints.length | ||
2494 | if (len <= MAX_ARGUMENTS_LENGTH) { | ||
2495 | return String.fromCharCode.apply(String, codePoints) // avoid extra slice() | ||
2496 | } | ||
2497 | |||
2498 | // Decode in chunks to avoid "call stack size exceeded". | ||
2499 | var res = '' | ||
2500 | var i = 0 | ||
2501 | while (i < len) { | ||
2502 | res += String.fromCharCode.apply( | ||
2503 | String, | ||
2504 | codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) | ||
2505 | ) | ||
2506 | } | ||
2507 | return res | ||
2508 | } | ||
2509 | |||
2510 | function asciiSlice (buf, start, end) { | ||
2511 | var ret = '' | ||
2512 | end = Math.min(buf.length, end) | ||
2513 | |||
2514 | for (var i = start; i < end; ++i) { | ||
2515 | ret += String.fromCharCode(buf[i] & 0x7F) | ||
2516 | } | ||
2517 | return ret | ||
2518 | } | ||
2519 | |||
2520 | function latin1Slice (buf, start, end) { | ||
2521 | var ret = '' | ||
2522 | end = Math.min(buf.length, end) | ||
2523 | |||
2524 | for (var i = start; i < end; ++i) { | ||
2525 | ret += String.fromCharCode(buf[i]) | ||
2526 | } | ||
2527 | return ret | ||
2528 | } | ||
2529 | |||
2530 | function hexSlice (buf, start, end) { | ||
2531 | var len = buf.length | ||
2532 | |||
2533 | if (!start || start < 0) start = 0 | ||
2534 | if (!end || end < 0 || end > len) end = len | ||
2535 | |||
2536 | var out = '' | ||
2537 | for (var i = start; i < end; ++i) { | ||
2538 | out += toHex(buf[i]) | ||
2539 | } | ||
2540 | return out | ||
2541 | } | ||
2542 | |||
2543 | function utf16leSlice (buf, start, end) { | ||
2544 | var bytes = buf.slice(start, end) | ||
2545 | var res = '' | ||
2546 | for (var i = 0; i < bytes.length; i += 2) { | ||
2547 | res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256)) | ||
2548 | } | ||
2549 | return res | ||
2550 | } | ||
2551 | |||
2552 | Buffer.prototype.slice = function slice (start, end) { | ||
2553 | var len = this.length | ||
2554 | start = ~~start | ||
2555 | end = end === undefined ? len : ~~end | ||
2556 | |||
2557 | if (start < 0) { | ||
2558 | start += len | ||
2559 | if (start < 0) start = 0 | ||
2560 | } else if (start > len) { | ||
2561 | start = len | ||
2562 | } | ||
2563 | |||
2564 | if (end < 0) { | ||
2565 | end += len | ||
2566 | if (end < 0) end = 0 | ||
2567 | } else if (end > len) { | ||
2568 | end = len | ||
2569 | } | ||
2570 | |||
2571 | if (end < start) end = start | ||
2572 | |||
2573 | var newBuf = this.subarray(start, end) | ||
2574 | // Return an augmented `Uint8Array` instance | ||
2575 | newBuf.__proto__ = Buffer.prototype | ||
2576 | return newBuf | ||
2577 | } | ||
2578 | |||
2579 | /* | ||
2580 | * Need to make sure that buffer isn't trying to write out of bounds. | ||
2581 | */ | ||
2582 | function checkOffset (offset, ext, length) { | ||
2583 | if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') | ||
2584 | if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') | ||
2585 | } | ||
2586 | |||
2587 | Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { | ||
2588 | offset = offset >>> 0 | ||
2589 | byteLength = byteLength >>> 0 | ||
2590 | if (!noAssert) checkOffset(offset, byteLength, this.length) | ||
2591 | |||
2592 | var val = this[offset] | ||
2593 | var mul = 1 | ||
2594 | var i = 0 | ||
2595 | while (++i < byteLength && (mul *= 0x100)) { | ||
2596 | val += this[offset + i] * mul | ||
2597 | } | ||
2598 | |||
2599 | return val | ||
2600 | } | ||
2601 | |||
2602 | Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { | ||
2603 | offset = offset >>> 0 | ||
2604 | byteLength = byteLength >>> 0 | ||
2605 | if (!noAssert) { | ||
2606 | checkOffset(offset, byteLength, this.length) | ||
2607 | } | ||
2608 | |||
2609 | var val = this[offset + --byteLength] | ||
2610 | var mul = 1 | ||
2611 | while (byteLength > 0 && (mul *= 0x100)) { | ||
2612 | val += this[offset + --byteLength] * mul | ||
2613 | } | ||
2614 | |||
2615 | return val | ||
2616 | } | ||
2617 | |||
2618 | Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { | ||
2619 | offset = offset >>> 0 | ||
2620 | if (!noAssert) checkOffset(offset, 1, this.length) | ||
2621 | return this[offset] | ||
2622 | } | ||
2623 | |||
2624 | Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { | ||
2625 | offset = offset >>> 0 | ||
2626 | if (!noAssert) checkOffset(offset, 2, this.length) | ||
2627 | return this[offset] | (this[offset + 1] << 8) | ||
2628 | } | ||
2629 | |||
2630 | Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { | ||
2631 | offset = offset >>> 0 | ||
2632 | if (!noAssert) checkOffset(offset, 2, this.length) | ||
2633 | return (this[offset] << 8) | this[offset + 1] | ||
2634 | } | ||
2635 | |||
2636 | Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { | ||
2637 | offset = offset >>> 0 | ||
2638 | if (!noAssert) checkOffset(offset, 4, this.length) | ||
2639 | |||
2640 | return ((this[offset]) | | ||
2641 | (this[offset + 1] << 8) | | ||
2642 | (this[offset + 2] << 16)) + | ||
2643 | (this[offset + 3] * 0x1000000) | ||
2644 | } | ||
2645 | |||
2646 | Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { | ||
2647 | offset = offset >>> 0 | ||
2648 | if (!noAssert) checkOffset(offset, 4, this.length) | ||
2649 | |||
2650 | return (this[offset] * 0x1000000) + | ||
2651 | ((this[offset + 1] << 16) | | ||
2652 | (this[offset + 2] << 8) | | ||
2653 | this[offset + 3]) | ||
2654 | } | ||
2655 | |||
2656 | Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { | ||
2657 | offset = offset >>> 0 | ||
2658 | byteLength = byteLength >>> 0 | ||
2659 | if (!noAssert) checkOffset(offset, byteLength, this.length) | ||
2660 | |||
2661 | var val = this[offset] | ||
2662 | var mul = 1 | ||
2663 | var i = 0 | ||
2664 | while (++i < byteLength && (mul *= 0x100)) { | ||
2665 | val += this[offset + i] * mul | ||
2666 | } | ||
2667 | mul *= 0x80 | ||
2668 | |||
2669 | if (val >= mul) val -= Math.pow(2, 8 * byteLength) | ||
2670 | |||
2671 | return val | ||
2672 | } | ||
2673 | |||
2674 | Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { | ||
2675 | offset = offset >>> 0 | ||
2676 | byteLength = byteLength >>> 0 | ||
2677 | if (!noAssert) checkOffset(offset, byteLength, this.length) | ||
2678 | |||
2679 | var i = byteLength | ||
2680 | var mul = 1 | ||
2681 | var val = this[offset + --i] | ||
2682 | while (i > 0 && (mul *= 0x100)) { | ||
2683 | val += this[offset + --i] * mul | ||
2684 | } | ||
2685 | mul *= 0x80 | ||
2686 | |||
2687 | if (val >= mul) val -= Math.pow(2, 8 * byteLength) | ||
2688 | |||
2689 | return val | ||
2690 | } | ||
2691 | |||
2692 | Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { | ||
2693 | offset = offset >>> 0 | ||
2694 | if (!noAssert) checkOffset(offset, 1, this.length) | ||
2695 | if (!(this[offset] & 0x80)) return (this[offset]) | ||
2696 | return ((0xff - this[offset] + 1) * -1) | ||
2697 | } | ||
2698 | |||
2699 | Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { | ||
2700 | offset = offset >>> 0 | ||
2701 | if (!noAssert) checkOffset(offset, 2, this.length) | ||
2702 | var val = this[offset] | (this[offset + 1] << 8) | ||
2703 | return (val & 0x8000) ? val | 0xFFFF0000 : val | ||
2704 | } | ||
2705 | |||
2706 | Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { | ||
2707 | offset = offset >>> 0 | ||
2708 | if (!noAssert) checkOffset(offset, 2, this.length) | ||
2709 | var val = this[offset + 1] | (this[offset] << 8) | ||
2710 | return (val & 0x8000) ? val | 0xFFFF0000 : val | ||
2711 | } | ||
2712 | |||
2713 | Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { | ||
2714 | offset = offset >>> 0 | ||
2715 | if (!noAssert) checkOffset(offset, 4, this.length) | ||
2716 | |||
2717 | return (this[offset]) | | ||
2718 | (this[offset + 1] << 8) | | ||
2719 | (this[offset + 2] << 16) | | ||
2720 | (this[offset + 3] << 24) | ||
2721 | } | ||
2722 | |||
2723 | Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { | ||
2724 | offset = offset >>> 0 | ||
2725 | if (!noAssert) checkOffset(offset, 4, this.length) | ||
2726 | |||
2727 | return (this[offset] << 24) | | ||
2728 | (this[offset + 1] << 16) | | ||
2729 | (this[offset + 2] << 8) | | ||
2730 | (this[offset + 3]) | ||
2731 | } | ||
2732 | |||
2733 | Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { | ||
2734 | offset = offset >>> 0 | ||
2735 | if (!noAssert) checkOffset(offset, 4, this.length) | ||
2736 | return ieee754.read(this, offset, true, 23, 4) | ||
2737 | } | ||
2738 | |||
2739 | Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { | ||
2740 | offset = offset >>> 0 | ||
2741 | if (!noAssert) checkOffset(offset, 4, this.length) | ||
2742 | return ieee754.read(this, offset, false, 23, 4) | ||
2743 | } | ||
2744 | |||
2745 | Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { | ||
2746 | offset = offset >>> 0 | ||
2747 | if (!noAssert) checkOffset(offset, 8, this.length) | ||
2748 | return ieee754.read(this, offset, true, 52, 8) | ||
2749 | } | ||
2750 | |||
2751 | Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { | ||
2752 | offset = offset >>> 0 | ||
2753 | if (!noAssert) checkOffset(offset, 8, this.length) | ||
2754 | return ieee754.read(this, offset, false, 52, 8) | ||
2755 | } | ||
2756 | |||
2757 | function checkInt (buf, value, offset, ext, max, min) { | ||
2758 | if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance') | ||
2759 | if (value > max || value < min) throw new RangeError('"value" argument is out of bounds') | ||
2760 | if (offset + ext > buf.length) throw new RangeError('Index out of range') | ||
2761 | } | ||
2762 | |||
2763 | Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { | ||
2764 | value = +value | ||
2765 | offset = offset >>> 0 | ||
2766 | byteLength = byteLength >>> 0 | ||
2767 | if (!noAssert) { | ||
2768 | var maxBytes = Math.pow(2, 8 * byteLength) - 1 | ||
2769 | checkInt(this, value, offset, byteLength, maxBytes, 0) | ||
2770 | } | ||
2771 | |||
2772 | var mul = 1 | ||
2773 | var i = 0 | ||
2774 | this[offset] = value & 0xFF | ||
2775 | while (++i < byteLength && (mul *= 0x100)) { | ||
2776 | this[offset + i] = (value / mul) & 0xFF | ||
2777 | } | ||
2778 | |||
2779 | return offset + byteLength | ||
2780 | } | ||
2781 | |||
2782 | Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { | ||
2783 | value = +value | ||
2784 | offset = offset >>> 0 | ||
2785 | byteLength = byteLength >>> 0 | ||
2786 | if (!noAssert) { | ||
2787 | var maxBytes = Math.pow(2, 8 * byteLength) - 1 | ||
2788 | checkInt(this, value, offset, byteLength, maxBytes, 0) | ||
2789 | } | ||
2790 | |||
2791 | var i = byteLength - 1 | ||
2792 | var mul = 1 | ||
2793 | this[offset + i] = value & 0xFF | ||
2794 | while (--i >= 0 && (mul *= 0x100)) { | ||
2795 | this[offset + i] = (value / mul) & 0xFF | ||
2796 | } | ||
2797 | |||
2798 | return offset + byteLength | ||
2799 | } | ||
2800 | |||
2801 | Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { | ||
2802 | value = +value | ||
2803 | offset = offset >>> 0 | ||
2804 | if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) | ||
2805 | this[offset] = (value & 0xff) | ||
2806 | return offset + 1 | ||
2807 | } | ||
2808 | |||
2809 | Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { | ||
2810 | value = +value | ||
2811 | offset = offset >>> 0 | ||
2812 | if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) | ||
2813 | this[offset] = (value & 0xff) | ||
2814 | this[offset + 1] = (value >>> 8) | ||
2815 | return offset + 2 | ||
2816 | } | ||
2817 | |||
2818 | Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { | ||
2819 | value = +value | ||
2820 | offset = offset >>> 0 | ||
2821 | if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) | ||
2822 | this[offset] = (value >>> 8) | ||
2823 | this[offset + 1] = (value & 0xff) | ||
2824 | return offset + 2 | ||
2825 | } | ||
2826 | |||
2827 | Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { | ||
2828 | value = +value | ||
2829 | offset = offset >>> 0 | ||
2830 | if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) | ||
2831 | this[offset + 3] = (value >>> 24) | ||
2832 | this[offset + 2] = (value >>> 16) | ||
2833 | this[offset + 1] = (value >>> 8) | ||
2834 | this[offset] = (value & 0xff) | ||
2835 | return offset + 4 | ||
2836 | } | ||
2837 | |||
2838 | Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { | ||
2839 | value = +value | ||
2840 | offset = offset >>> 0 | ||
2841 | if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) | ||
2842 | this[offset] = (value >>> 24) | ||
2843 | this[offset + 1] = (value >>> 16) | ||
2844 | this[offset + 2] = (value >>> 8) | ||
2845 | this[offset + 3] = (value & 0xff) | ||
2846 | return offset + 4 | ||
2847 | } | ||
2848 | |||
2849 | Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { | ||
2850 | value = +value | ||
2851 | offset = offset >>> 0 | ||
2852 | if (!noAssert) { | ||
2853 | var limit = Math.pow(2, (8 * byteLength) - 1) | ||
2854 | |||
2855 | checkInt(this, value, offset, byteLength, limit - 1, -limit) | ||
2856 | } | ||
2857 | |||
2858 | var i = 0 | ||
2859 | var mul = 1 | ||
2860 | var sub = 0 | ||
2861 | this[offset] = value & 0xFF | ||
2862 | while (++i < byteLength && (mul *= 0x100)) { | ||
2863 | if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { | ||
2864 | sub = 1 | ||
2865 | } | ||
2866 | this[offset + i] = ((value / mul) >> 0) - sub & 0xFF | ||
2867 | } | ||
2868 | |||
2869 | return offset + byteLength | ||
2870 | } | ||
2871 | |||
2872 | Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { | ||
2873 | value = +value | ||
2874 | offset = offset >>> 0 | ||
2875 | if (!noAssert) { | ||
2876 | var limit = Math.pow(2, (8 * byteLength) - 1) | ||
2877 | |||
2878 | checkInt(this, value, offset, byteLength, limit - 1, -limit) | ||
2879 | } | ||
2880 | |||
2881 | var i = byteLength - 1 | ||
2882 | var mul = 1 | ||
2883 | var sub = 0 | ||
2884 | this[offset + i] = value & 0xFF | ||
2885 | while (--i >= 0 && (mul *= 0x100)) { | ||
2886 | if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { | ||
2887 | sub = 1 | ||
2888 | } | ||
2889 | this[offset + i] = ((value / mul) >> 0) - sub & 0xFF | ||
2890 | } | ||
2891 | |||
2892 | return offset + byteLength | ||
2893 | } | ||
2894 | |||
2895 | Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { | ||
2896 | value = +value | ||
2897 | offset = offset >>> 0 | ||
2898 | if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) | ||
2899 | if (value < 0) value = 0xff + value + 1 | ||
2900 | this[offset] = (value & 0xff) | ||
2901 | return offset + 1 | ||
2902 | } | ||
2903 | |||
2904 | Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { | ||
2905 | value = +value | ||
2906 | offset = offset >>> 0 | ||
2907 | if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) | ||
2908 | this[offset] = (value & 0xff) | ||
2909 | this[offset + 1] = (value >>> 8) | ||
2910 | return offset + 2 | ||
2911 | } | ||
2912 | |||
2913 | Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { | ||
2914 | value = +value | ||
2915 | offset = offset >>> 0 | ||
2916 | if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) | ||
2917 | this[offset] = (value >>> 8) | ||
2918 | this[offset + 1] = (value & 0xff) | ||
2919 | return offset + 2 | ||
2920 | } | ||
2921 | |||
2922 | Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { | ||
2923 | value = +value | ||
2924 | offset = offset >>> 0 | ||
2925 | if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) | ||
2926 | this[offset] = (value & 0xff) | ||
2927 | this[offset + 1] = (value >>> 8) | ||
2928 | this[offset + 2] = (value >>> 16) | ||
2929 | this[offset + 3] = (value >>> 24) | ||
2930 | return offset + 4 | ||
2931 | } | ||
2932 | |||
2933 | Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { | ||
2934 | value = +value | ||
2935 | offset = offset >>> 0 | ||
2936 | if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) | ||
2937 | if (value < 0) value = 0xffffffff + value + 1 | ||
2938 | this[offset] = (value >>> 24) | ||
2939 | this[offset + 1] = (value >>> 16) | ||
2940 | this[offset + 2] = (value >>> 8) | ||
2941 | this[offset + 3] = (value & 0xff) | ||
2942 | return offset + 4 | ||
2943 | } | ||
2944 | |||
2945 | function checkIEEE754 (buf, value, offset, ext, max, min) { | ||
2946 | if (offset + ext > buf.length) throw new RangeError('Index out of range') | ||
2947 | if (offset < 0) throw new RangeError('Index out of range') | ||
2948 | } | ||
2949 | |||
2950 | function writeFloat (buf, value, offset, littleEndian, noAssert) { | ||
2951 | value = +value | ||
2952 | offset = offset >>> 0 | ||
2953 | if (!noAssert) { | ||
2954 | checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) | ||
2955 | } | ||
2956 | ieee754.write(buf, value, offset, littleEndian, 23, 4) | ||
2957 | return offset + 4 | ||
2958 | } | ||
2959 | |||
2960 | Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { | ||
2961 | return writeFloat(this, value, offset, true, noAssert) | ||
2962 | } | ||
2963 | |||
2964 | Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { | ||
2965 | return writeFloat(this, value, offset, false, noAssert) | ||
2966 | } | ||
2967 | |||
2968 | function writeDouble (buf, value, offset, littleEndian, noAssert) { | ||
2969 | value = +value | ||
2970 | offset = offset >>> 0 | ||
2971 | if (!noAssert) { | ||
2972 | checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) | ||
2973 | } | ||
2974 | ieee754.write(buf, value, offset, littleEndian, 52, 8) | ||
2975 | return offset + 8 | ||
2976 | } | ||
2977 | |||
2978 | Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { | ||
2979 | return writeDouble(this, value, offset, true, noAssert) | ||
2980 | } | ||
2981 | |||
2982 | Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { | ||
2983 | return writeDouble(this, value, offset, false, noAssert) | ||
2984 | } | ||
2985 | |||
2986 | // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) | ||
2987 | Buffer.prototype.copy = function copy (target, targetStart, start, end) { | ||
2988 | if (!start) start = 0 | ||
2989 | if (!end && end !== 0) end = this.length | ||
2990 | if (targetStart >= target.length) targetStart = target.length | ||
2991 | if (!targetStart) targetStart = 0 | ||
2992 | if (end > 0 && end < start) end = start | ||
2993 | |||
2994 | // Copy 0 bytes; we're done | ||
2995 | if (end === start) return 0 | ||
2996 | if (target.length === 0 || this.length === 0) return 0 | ||
2997 | |||
2998 | // Fatal error conditions | ||
2999 | if (targetStart < 0) { | ||
3000 | throw new RangeError('targetStart out of bounds') | ||
3001 | } | ||
3002 | if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds') | ||
3003 | if (end < 0) throw new RangeError('sourceEnd out of bounds') | ||
3004 | |||
3005 | // Are we oob? | ||
3006 | if (end > this.length) end = this.length | ||
3007 | if (target.length - targetStart < end - start) { | ||
3008 | end = target.length - targetStart + start | ||
3009 | } | ||
3010 | |||
3011 | var len = end - start | ||
3012 | var i | ||
3013 | |||
3014 | if (this === target && start < targetStart && targetStart < end) { | ||
3015 | // descending copy from end | ||
3016 | for (i = len - 1; i >= 0; --i) { | ||
3017 | target[i + targetStart] = this[i + start] | ||
3018 | } | ||
3019 | } else if (len < 1000) { | ||
3020 | // ascending copy from start | ||
3021 | for (i = 0; i < len; ++i) { | ||
3022 | target[i + targetStart] = this[i + start] | ||
3023 | } | ||
3024 | } else { | ||
3025 | Uint8Array.prototype.set.call( | ||
3026 | target, | ||
3027 | this.subarray(start, start + len), | ||
3028 | targetStart | ||
3029 | ) | ||
3030 | } | ||
3031 | |||
3032 | return len | ||
3033 | } | ||
3034 | |||
3035 | // Usage: | ||
3036 | // buffer.fill(number[, offset[, end]]) | ||
3037 | // buffer.fill(buffer[, offset[, end]]) | ||
3038 | // buffer.fill(string[, offset[, end]][, encoding]) | ||
3039 | Buffer.prototype.fill = function fill (val, start, end, encoding) { | ||
3040 | // Handle string cases: | ||
3041 | if (typeof val === 'string') { | ||
3042 | if (typeof start === 'string') { | ||
3043 | encoding = start | ||
3044 | start = 0 | ||
3045 | end = this.length | ||
3046 | } else if (typeof end === 'string') { | ||
3047 | encoding = end | ||
3048 | end = this.length | ||
3049 | } | ||
3050 | if (val.length === 1) { | ||
3051 | var code = val.charCodeAt(0) | ||
3052 | if (code < 256) { | ||
3053 | val = code | ||
3054 | } | ||
3055 | } | ||
3056 | if (encoding !== undefined && typeof encoding !== 'string') { | ||
3057 | throw new TypeError('encoding must be a string') | ||
3058 | } | ||
3059 | if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { | ||
3060 | throw new TypeError('Unknown encoding: ' + encoding) | ||
3061 | } | ||
3062 | } else if (typeof val === 'number') { | ||
3063 | val = val & 255 | ||
3064 | } | ||
3065 | |||
3066 | // Invalid ranges are not set to a default, so can range check early. | ||
3067 | if (start < 0 || this.length < start || this.length < end) { | ||
3068 | throw new RangeError('Out of range index') | ||
3069 | } | ||
3070 | |||
3071 | if (end <= start) { | ||
3072 | return this | ||
3073 | } | ||
3074 | |||
3075 | start = start >>> 0 | ||
3076 | end = end === undefined ? this.length : end >>> 0 | ||
3077 | |||
3078 | if (!val) val = 0 | ||
3079 | |||
3080 | var i | ||
3081 | if (typeof val === 'number') { | ||
3082 | for (i = start; i < end; ++i) { | ||
3083 | this[i] = val | ||
3084 | } | ||
3085 | } else { | ||
3086 | var bytes = Buffer.isBuffer(val) | ||
3087 | ? val | ||
3088 | : new Buffer(val, encoding) | ||
3089 | var len = bytes.length | ||
3090 | for (i = 0; i < end - start; ++i) { | ||
3091 | this[i + start] = bytes[i % len] | ||
3092 | } | ||
3093 | } | ||
3094 | |||
3095 | return this | ||
3096 | } | ||
3097 | |||
3098 | // HELPER FUNCTIONS | ||
3099 | // ================ | ||
3100 | |||
3101 | var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g | ||
3102 | |||
3103 | function base64clean (str) { | ||
3104 | // Node strips out invalid characters like \n and \t from the string, base64-js does not | ||
3105 | str = str.trim().replace(INVALID_BASE64_RE, '') | ||
3106 | // Node converts strings with length < 2 to '' | ||
3107 | if (str.length < 2) return '' | ||
3108 | // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not | ||
3109 | while (str.length % 4 !== 0) { | ||
3110 | str = str + '=' | ||
3111 | } | ||
3112 | return str | ||
3113 | } | ||
3114 | |||
3115 | function toHex (n) { | ||
3116 | if (n < 16) return '0' + n.toString(16) | ||
3117 | return n.toString(16) | ||
3118 | } | ||
3119 | |||
3120 | function utf8ToBytes (string, units) { | ||
3121 | units = units || Infinity | ||
3122 | var codePoint | ||
3123 | var length = string.length | ||
3124 | var leadSurrogate = null | ||
3125 | var bytes = [] | ||
3126 | |||
3127 | for (var i = 0; i < length; ++i) { | ||
3128 | codePoint = string.charCodeAt(i) | ||
3129 | |||
3130 | // is surrogate component | ||
3131 | if (codePoint > 0xD7FF && codePoint < 0xE000) { | ||
3132 | // last char was a lead | ||
3133 | if (!leadSurrogate) { | ||
3134 | // no lead yet | ||
3135 | if (codePoint > 0xDBFF) { | ||
3136 | // unexpected trail | ||
3137 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) | ||
3138 | continue | ||
3139 | } else if (i + 1 === length) { | ||
3140 | // unpaired lead | ||
3141 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) | ||
3142 | continue | ||
3143 | } | ||
3144 | |||
3145 | // valid lead | ||
3146 | leadSurrogate = codePoint | ||
3147 | |||
3148 | continue | ||
3149 | } | ||
3150 | |||
3151 | // 2 leads in a row | ||
3152 | if (codePoint < 0xDC00) { | ||
3153 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) | ||
3154 | leadSurrogate = codePoint | ||
3155 | continue | ||
3156 | } | ||
3157 | |||
3158 | // valid surrogate pair | ||
3159 | codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 | ||
3160 | } else if (leadSurrogate) { | ||
3161 | // valid bmp char, but last char was a lead | ||
3162 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) | ||
3163 | } | ||
3164 | |||
3165 | leadSurrogate = null | ||
3166 | |||
3167 | // encode utf8 | ||
3168 | if (codePoint < 0x80) { | ||
3169 | if ((units -= 1) < 0) break | ||
3170 | bytes.push(codePoint) | ||
3171 | } else if (codePoint < 0x800) { | ||
3172 | if ((units -= 2) < 0) break | ||
3173 | bytes.push( | ||
3174 | codePoint >> 0x6 | 0xC0, | ||
3175 | codePoint & 0x3F | 0x80 | ||
3176 | ) | ||
3177 | } else if (codePoint < 0x10000) { | ||
3178 | if ((units -= 3) < 0) break | ||
3179 | bytes.push( | ||
3180 | codePoint >> 0xC | 0xE0, | ||
3181 | codePoint >> 0x6 & 0x3F | 0x80, | ||
3182 | codePoint & 0x3F | 0x80 | ||
3183 | ) | ||
3184 | } else if (codePoint < 0x110000) { | ||
3185 | if ((units -= 4) < 0) break | ||
3186 | bytes.push( | ||
3187 | codePoint >> 0x12 | 0xF0, | ||
3188 | codePoint >> 0xC & 0x3F | 0x80, | ||
3189 | codePoint >> 0x6 & 0x3F | 0x80, | ||
3190 | codePoint & 0x3F | 0x80 | ||
3191 | ) | ||
3192 | } else { | ||
3193 | throw new Error('Invalid code point') | ||
3194 | } | ||
3195 | } | ||
3196 | |||
3197 | return bytes | ||
3198 | } | ||
3199 | |||
3200 | function asciiToBytes (str) { | ||
3201 | var byteArray = [] | ||
3202 | for (var i = 0; i < str.length; ++i) { | ||
3203 | // Node's code seems to be doing this and not & 0x7F.. | ||
3204 | byteArray.push(str.charCodeAt(i) & 0xFF) | ||
3205 | } | ||
3206 | return byteArray | ||
3207 | } | ||
3208 | |||
3209 | function utf16leToBytes (str, units) { | ||
3210 | var c, hi, lo | ||
3211 | var byteArray = [] | ||
3212 | for (var i = 0; i < str.length; ++i) { | ||
3213 | if ((units -= 2) < 0) break | ||
3214 | |||
3215 | c = str.charCodeAt(i) | ||
3216 | hi = c >> 8 | ||
3217 | lo = c % 256 | ||
3218 | byteArray.push(lo) | ||
3219 | byteArray.push(hi) | ||
3220 | } | ||
3221 | |||
3222 | return byteArray | ||
3223 | } | ||
3224 | |||
3225 | function base64ToBytes (str) { | ||
3226 | return base64.toByteArray(base64clean(str)) | ||
3227 | } | ||
3228 | |||
3229 | function blitBuffer (src, dst, offset, length) { | ||
3230 | for (var i = 0; i < length; ++i) { | ||
3231 | if ((i + offset >= dst.length) || (i >= src.length)) break | ||
3232 | dst[i + offset] = src[i] | ||
3233 | } | ||
3234 | return i | ||
3235 | } | ||
3236 | |||
3237 | // ArrayBuffers from another context (i.e. an iframe) do not pass the `instanceof` check | ||
3238 | // but they should be treated as valid. See: https://github.com/feross/buffer/issues/166 | ||
3239 | function isArrayBuffer (obj) { | ||
3240 | return obj instanceof ArrayBuffer || | ||
3241 | (obj != null && obj.constructor != null && obj.constructor.name === 'ArrayBuffer' && | ||
3242 | typeof obj.byteLength === 'number') | ||
3243 | } | ||
3244 | |||
3245 | // Node 0.10 supports `ArrayBuffer` but lacks `ArrayBuffer.isView` | ||
3246 | function isArrayBufferView (obj) { | ||
3247 | return (typeof ArrayBuffer.isView === 'function') && ArrayBuffer.isView(obj) | ||
3248 | } | ||
3249 | |||
3250 | function numberIsNaN (obj) { | ||
3251 | return obj !== obj // eslint-disable-line no-self-compare | ||
3252 | } | ||
3253 | |||
3254 | },{"base64-js":2,"ieee754":20}],9:[function(require,module,exports){ | ||
3255 | /** | ||
3256 | * @license | ||
3257 | * https://github.com/bitcoincashjs/cashaddr | ||
3258 | * Copyright (c) 2017-2018 Emilio Almansi | ||
3259 | * Distributed under the MIT software license, see the accompanying | ||
3260 | * file LICENSE or http://www.opensource.org/licenses/mit-license.php. | ||
3261 | */ | ||
3262 | |||
3263 | 'use strict'; | ||
3264 | |||
3265 | var validate = require('./validation').validate; | ||
3266 | |||
3267 | /** | ||
3268 | * Base32 encoding and decoding. | ||
3269 | * | ||
3270 | * @module base32 | ||
3271 | */ | ||
3272 | |||
3273 | /** | ||
3274 | * Charset containing the 32 symbols used in the base32 encoding. | ||
3275 | * @private | ||
3276 | */ | ||
3277 | var CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; | ||
3278 | |||
3279 | /** | ||
3280 | * Inverted index mapping each symbol into its index within the charset. | ||
3281 | * @private | ||
3282 | */ | ||
3283 | var CHARSET_INVERSE_INDEX = { | ||
3284 | 'q': 0, 'p': 1, 'z': 2, 'r': 3, 'y': 4, '9': 5, 'x': 6, '8': 7, | ||
3285 | 'g': 8, 'f': 9, '2': 10, 't': 11, 'v': 12, 'd': 13, 'w': 14, '0': 15, | ||
3286 | 's': 16, '3': 17, 'j': 18, 'n': 19, '5': 20, '4': 21, 'k': 22, 'h': 23, | ||
3287 | 'c': 24, 'e': 25, '6': 26, 'm': 27, 'u': 28, 'a': 29, '7': 30, 'l': 31, | ||
3288 | }; | ||
3289 | |||
3290 | /** | ||
3291 | * Encodes the given array of 5-bit integers as a base32-encoded string. | ||
3292 | * | ||
3293 | * @static | ||
3294 | * @param {Uint8Array} data Array of integers between 0 and 31 inclusive. | ||
3295 | * @returns {string} | ||
3296 | * @throws {ValidationError} | ||
3297 | */ | ||
3298 | function encode(data) { | ||
3299 | validate(data instanceof Uint8Array, 'Invalid data: ' + data + '.'); | ||
3300 | var base32 = ''; | ||
3301 | for (var i = 0; i < data.length; ++i) { | ||
3302 | var value = data[i]; | ||
3303 | validate(0 <= value && value < 32, 'Invalid value: ' + value + '.'); | ||
3304 | base32 += CHARSET[value]; | ||
3305 | } | ||
3306 | return base32; | ||
3307 | } | ||
3308 | |||
3309 | /** | ||
3310 | * Decodes the given base32-encoded string into an array of 5-bit integers. | ||
3311 | * | ||
3312 | * @static | ||
3313 | * @param {string} string | ||
3314 | * @returns {Uint8Array} | ||
3315 | * @throws {ValidationError} | ||
3316 | */ | ||
3317 | function decode(string) { | ||
3318 | validate(typeof string === 'string', 'Invalid base32-encoded string: ' + string + '.'); | ||
3319 | var data = new Uint8Array(string.length); | ||
3320 | for (var i = 0; i < string.length; ++i) { | ||
3321 | var value = string[i]; | ||
3322 | validate(value in CHARSET_INVERSE_INDEX, 'Invalid value: ' + value + '.'); | ||
3323 | data[i] = CHARSET_INVERSE_INDEX[value]; | ||
3324 | } | ||
3325 | return data; | ||
3326 | } | ||
3327 | |||
3328 | module.exports = { | ||
3329 | encode: encode, | ||
3330 | decode: decode, | ||
3331 | }; | ||
3332 | |||
3333 | },{"./validation":12}],10:[function(require,module,exports){ | ||
3334 | /** | ||
3335 | * @license | ||
3336 | * https://github.com/bitcoincashjs/cashaddr | ||
3337 | * Copyright (c) 2017-2018 Emilio Almansi | ||
3338 | * Distributed under the MIT software license, see the accompanying | ||
3339 | * file LICENSE or http://www.opensource.org/licenses/mit-license.php. | ||
3340 | */ | ||
3341 | |||
3342 | 'use strict'; | ||
3343 | |||
3344 | var base32 = require('./base32'); | ||
3345 | var bigInt = require('big-integer'); | ||
3346 | var convertBits = require('./convertBits'); | ||
3347 | var validation = require('./validation'); | ||
3348 | var validate = validation.validate; | ||
3349 | |||
3350 | /** | ||
3351 | * Encoding and decoding of the new Cash Address format for Bitcoin Cash. <br /> | ||
3352 | * Compliant with the original cashaddr specification: | ||
3353 | * {@link https://github.com/Bitcoin-UAHF/spec/blob/master/cashaddr.md} | ||
3354 | * @module cashaddr | ||
3355 | */ | ||
3356 | |||
3357 | /** | ||
3358 | * Encodes a hash from a given type into a Bitcoin Cash address with the given prefix. | ||
3359 | * | ||
3360 | * @static | ||
3361 | * @param {string} prefix Network prefix. E.g.: 'bitcoincash'. | ||
3362 | * @param {string} type Type of address to generate. Either 'P2PKH' or 'P2SH'. | ||
3363 | * @param {Uint8Array} hash Hash to encode represented as an array of 8-bit integers. | ||
3364 | * @returns {string} | ||
3365 | * @throws {ValidationError} | ||
3366 | */ | ||
3367 | function encode(prefix, type, hash) { | ||
3368 | validate(typeof prefix === 'string' && isValidPrefix(prefix), 'Invalid prefix: ' + prefix + '.'); | ||
3369 | validate(typeof type === 'string', 'Invalid type: ' + type + '.'); | ||
3370 | validate(hash instanceof Uint8Array, 'Invalid hash: ' + hash + '.'); | ||
3371 | var prefixData = concat(prefixToUint5Array(prefix), new Uint8Array(1)); | ||
3372 | var versionByte = getTypeBits(type) + getHashSizeBits(hash); | ||
3373 | var payloadData = toUint5Array(concat(Uint8Array.of(versionByte), hash)); | ||
3374 | var checksumData = concat(concat(prefixData, payloadData), new Uint8Array(8)); | ||
3375 | var payload = concat(payloadData, checksumToUint5Array(polymod(checksumData))); | ||
3376 | return prefix + ':' + base32.encode(payload); | ||
3377 | } | ||
3378 | |||
3379 | /** | ||
3380 | * Decodes the given address into its constituting prefix, type and hash. See [#encode()]{@link encode}. | ||
3381 | * | ||
3382 | * @static | ||
3383 | * @param {string} address Address to decode. E.g.: 'bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a'. | ||
3384 | * @returns {object} | ||
3385 | * @throws {ValidationError} | ||
3386 | */ | ||
3387 | function decode(address) { | ||
3388 | validate(typeof address === 'string' && hasSingleCase(address), 'Invalid address: ' + address + '.'); | ||
3389 | var pieces = address.toLowerCase().split(':'); | ||
3390 | validate(pieces.length === 2, 'Missing prefix: ' + address + '.'); | ||
3391 | var prefix = pieces[0]; | ||
3392 | var payload = base32.decode(pieces[1]); | ||
3393 | validate(validChecksum(prefix, payload), 'Invalid checksum: ' + address + '.'); | ||
3394 | var payloadData = fromUint5Array(payload.slice(0, -8)); | ||
3395 | var versionByte = payloadData[0]; | ||
3396 | var hash = payloadData.slice(1); | ||
3397 | validate(getHashSize(versionByte) === hash.length * 8, 'Invalid hash size: ' + address + '.'); | ||
3398 | var type = getType(versionByte); | ||
3399 | return { | ||
3400 | prefix: prefix, | ||
3401 | type: type, | ||
3402 | hash: hash, | ||
3403 | }; | ||
3404 | } | ||
3405 | |||
3406 | /** | ||
3407 | * Error thrown when encoding or decoding fail due to invalid input. | ||
3408 | * | ||
3409 | * @constructor ValidationError | ||
3410 | * @param {string} message Error description. | ||
3411 | */ | ||
3412 | var ValidationError = validation.ValidationError; | ||
3413 | |||
3414 | /** | ||
3415 | * Valid address prefixes. | ||
3416 | * | ||
3417 | * @private | ||
3418 | */ | ||
3419 | var VALID_PREFIXES = ['bitcoincash', 'bchtest', 'bchreg']; | ||
3420 | |||
3421 | /** | ||
3422 | * Checks whether a string is a valid prefix; ie., it has a single letter case | ||
3423 | * and is one of 'bitcoincash', 'bchtest', or 'bchreg'. | ||
3424 | * | ||
3425 | * @private | ||
3426 | * @param {string} prefix | ||
3427 | * @returns {boolean} | ||
3428 | */ | ||
3429 | function isValidPrefix(prefix) { | ||
3430 | return hasSingleCase(prefix) && VALID_PREFIXES.indexOf(prefix.toLowerCase()) !== -1; | ||
3431 | } | ||
3432 | |||
3433 | /** | ||
3434 | * Derives an array from the given prefix to be used in the computation | ||
3435 | * of the address' checksum. | ||
3436 | * | ||
3437 | * @private | ||
3438 | * @param {string} prefix Network prefix. E.g.: 'bitcoincash'. | ||
3439 | * @returns {Uint8Array} | ||
3440 | */ | ||
3441 | function prefixToUint5Array(prefix) { | ||
3442 | var result = new Uint8Array(prefix.length); | ||
3443 | for (var i = 0; i < prefix.length; ++i) { | ||
3444 | result[i] = prefix[i].charCodeAt(0) & 31; | ||
3445 | } | ||
3446 | return result; | ||
3447 | } | ||
3448 | |||
3449 | /** | ||
3450 | * Returns an array representation of the given checksum to be encoded | ||
3451 | * within the address' payload. | ||
3452 | * | ||
3453 | * @private | ||
3454 | * @param {BigInteger} checksum Computed checksum. | ||
3455 | * @returns {Uint8Array} | ||
3456 | */ | ||
3457 | function checksumToUint5Array(checksum) { | ||
3458 | var result = new Uint8Array(8); | ||
3459 | for (var i = 0; i < 8; ++i) { | ||
3460 | result[7 - i] = checksum.and(31).toJSNumber(); | ||
3461 | checksum = checksum.shiftRight(5); | ||
3462 | } | ||
3463 | return result; | ||
3464 | } | ||
3465 | |||
3466 | /** | ||
3467 | * Returns the bit representation of the given type within the version | ||
3468 | * byte. | ||
3469 | * | ||
3470 | * @private | ||
3471 | * @param {string} type Address type. Either 'P2PKH' or 'P2SH'. | ||
3472 | * @returns {number} | ||
3473 | * @throws {ValidationError} | ||
3474 | */ | ||
3475 | function getTypeBits(type) { | ||
3476 | switch (type) { | ||
3477 | case 'P2PKH': | ||
3478 | return 0; | ||
3479 | case 'P2SH': | ||
3480 | return 8; | ||
3481 | default: | ||
3482 | throw new ValidationError('Invalid type: ' + type + '.'); | ||
3483 | } | ||
3484 | } | ||
3485 | |||
3486 | /** | ||
3487 | * Retrieves the address type from its bit representation within the | ||
3488 | * version byte. | ||
3489 | * | ||
3490 | * @private | ||
3491 | * @param {number} versionByte | ||
3492 | * @returns {string} | ||
3493 | * @throws {ValidationError} | ||
3494 | */ | ||
3495 | function getType(versionByte) { | ||
3496 | switch (versionByte & 120) { | ||
3497 | case 0: | ||
3498 | return 'P2PKH'; | ||
3499 | case 8: | ||
3500 | return 'P2SH'; | ||
3501 | default: | ||
3502 | throw new ValidationError('Invalid address type in version byte: ' + versionByte + '.'); | ||
3503 | } | ||
3504 | } | ||
3505 | |||
3506 | /** | ||
3507 | * Returns the bit representation of the length in bits of the given | ||
3508 | * hash within the version byte. | ||
3509 | * | ||
3510 | * @private | ||
3511 | * @param {Uint8Array} hash Hash to encode represented as an array of 8-bit integers. | ||
3512 | * @returns {number} | ||
3513 | * @throws {ValidationError} | ||
3514 | */ | ||
3515 | function getHashSizeBits(hash) { | ||
3516 | switch (hash.length * 8) { | ||
3517 | case 160: | ||
3518 | return 0; | ||
3519 | case 192: | ||
3520 | return 1; | ||
3521 | case 224: | ||
3522 | return 2; | ||
3523 | case 256: | ||
3524 | return 3; | ||
3525 | case 320: | ||
3526 | return 4; | ||
3527 | case 384: | ||
3528 | return 5; | ||
3529 | case 448: | ||
3530 | return 6; | ||
3531 | case 512: | ||
3532 | return 7; | ||
3533 | default: | ||
3534 | throw new ValidationError('Invalid hash size: ' + hash.length + '.'); | ||
3535 | } | ||
3536 | } | ||
3537 | |||
3538 | /** | ||
3539 | * Retrieves the the length in bits of the encoded hash from its bit | ||
3540 | * representation within the version byte. | ||
3541 | * | ||
3542 | * @private | ||
3543 | * @param {number} versionByte | ||
3544 | * @returns {number} | ||
3545 | */ | ||
3546 | function getHashSize(versionByte) { | ||
3547 | switch (versionByte & 7) { | ||
3548 | case 0: | ||
3549 | return 160; | ||
3550 | case 1: | ||
3551 | return 192; | ||
3552 | case 2: | ||
3553 | return 224; | ||
3554 | case 3: | ||
3555 | return 256; | ||
3556 | case 4: | ||
3557 | return 320; | ||
3558 | case 5: | ||
3559 | return 384; | ||
3560 | case 6: | ||
3561 | return 448; | ||
3562 | case 7: | ||
3563 | return 512; | ||
3564 | } | ||
3565 | } | ||
3566 | |||
3567 | /** | ||
3568 | * Converts an array of 8-bit integers into an array of 5-bit integers, | ||
3569 | * right-padding with zeroes if necessary. | ||
3570 | * | ||
3571 | * @private | ||
3572 | * @param {Uint8Array} data | ||
3573 | * @returns {Uint8Array} | ||
3574 | */ | ||
3575 | function toUint5Array(data) { | ||
3576 | return convertBits(data, 8, 5); | ||
3577 | } | ||
3578 | |||
3579 | /** | ||
3580 | * Converts an array of 5-bit integers back into an array of 8-bit integers, | ||
3581 | * removing extra zeroes left from padding if necessary. | ||
3582 | * Throws a {@link ValidationError} if input is not a zero-padded array of 8-bit integers. | ||
3583 | * | ||
3584 | * @private | ||
3585 | * @param {Uint8Array} data | ||
3586 | * @returns {Uint8Array} | ||
3587 | * @throws {ValidationError} | ||
3588 | */ | ||
3589 | function fromUint5Array(data) { | ||
3590 | return convertBits(data, 5, 8, true); | ||
3591 | } | ||
3592 | |||
3593 | /** | ||
3594 | * Returns the concatenation a and b. | ||
3595 | * | ||
3596 | * @private | ||
3597 | * @param {Uint8Array} a | ||
3598 | * @param {Uint8Array} b | ||
3599 | * @returns {Uint8Array} | ||
3600 | * @throws {ValidationError} | ||
3601 | */ | ||
3602 | function concat(a, b) { | ||
3603 | var ab = new Uint8Array(a.length + b.length); | ||
3604 | ab.set(a); | ||
3605 | ab.set(b, a.length); | ||
3606 | return ab; | ||
3607 | } | ||
3608 | |||
3609 | /** | ||
3610 | * Computes a checksum from the given input data as specified for the CashAddr | ||
3611 | * format: https://github.com/Bitcoin-UAHF/spec/blob/master/cashaddr.md. | ||
3612 | * | ||
3613 | * @private | ||
3614 | * @param {Uint8Array} data Array of 5-bit integers over which the checksum is to be computed. | ||
3615 | * @returns {BigInteger} | ||
3616 | */ | ||
3617 | function polymod(data) { | ||
3618 | var GENERATOR = [0x98f2bc8e61, 0x79b76d99e2, 0xf33e5fb3c4, 0xae2eabe2a8, 0x1e4f43e470]; | ||
3619 | var checksum = bigInt(1); | ||
3620 | for (var i = 0; i < data.length; ++i) { | ||
3621 | var value = data[i]; | ||
3622 | var topBits = checksum.shiftRight(35); | ||
3623 | checksum = checksum.and(0x07ffffffff).shiftLeft(5).xor(value); | ||
3624 | for (var j = 0; j < GENERATOR.length; ++j) { | ||
3625 | if (topBits.shiftRight(j).and(1).equals(1)) { | ||
3626 | checksum = checksum.xor(GENERATOR[j]); | ||
3627 | } | ||
3628 | } | ||
3629 | } | ||
3630 | return checksum.xor(1); | ||
3631 | } | ||
3632 | |||
3633 | /** | ||
3634 | * Verify that the payload has not been corrupted by checking that the | ||
3635 | * checksum is valid. | ||
3636 | * | ||
3637 | * @private | ||
3638 | * @param {string} prefix Network prefix. E.g.: 'bitcoincash'. | ||
3639 | * @param {Uint8Array} payload Array of 5-bit integers containing the address' payload. | ||
3640 | * @returns {boolean} | ||
3641 | */ | ||
3642 | function validChecksum(prefix, payload) { | ||
3643 | var prefixData = concat(prefixToUint5Array(prefix), new Uint8Array(1)); | ||
3644 | var checksumData = concat(prefixData, payload); | ||
3645 | return polymod(checksumData).equals(0); | ||
3646 | } | ||
3647 | |||
3648 | /** | ||
3649 | * Returns true if, and only if, the given string contains either uppercase | ||
3650 | * or lowercase letters, but not both. | ||
3651 | * | ||
3652 | * @private | ||
3653 | * @param {string} string Input string. | ||
3654 | * @returns {boolean} | ||
3655 | */ | ||
3656 | function hasSingleCase(string) { | ||
3657 | return string === string.toLowerCase() || string === string.toUpperCase(); | ||
3658 | } | ||
3659 | |||
3660 | module.exports = { | ||
3661 | encode: encode, | ||
3662 | decode: decode, | ||
3663 | ValidationError: ValidationError, | ||
3664 | }; | ||
3665 | |||
3666 | },{"./base32":9,"./convertBits":11,"./validation":12,"big-integer":3}],11:[function(require,module,exports){ | ||
3667 | // Copyright (c) 2017-2018 Emilio Almansi | ||
3668 | // Copyright (c) 2017 Pieter Wuille | ||
3669 | // | ||
3670 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
3671 | // of this software and associated documentation files (the "Software"), to deal | ||
3672 | // in the Software without restriction, including without limitation the rights | ||
3673 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
3674 | // copies of the Software, and to permit persons to whom the Software is | ||
3675 | // furnished to do so, subject to the following conditions: | ||
3676 | // | ||
3677 | // The above copyright notice and this permission notice shall be included in | ||
3678 | // all copies or substantial portions of the Software. | ||
3679 | // | ||
3680 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
3681 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
3682 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
3683 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
3684 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
3685 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
3686 | // THE SOFTWARE. | ||
3687 | |||
3688 | 'use strict'; | ||
3689 | |||
3690 | var validate = require('./validation').validate; | ||
3691 | |||
3692 | /** | ||
3693 | * Converts an array of integers made up of 'from' bits into an | ||
3694 | * array of integers made up of 'to' bits. The output array is | ||
3695 | * zero-padded if necessary, unless strict mode is true. | ||
3696 | * Throws a {@link ValidationError} if input is invalid. | ||
3697 | * Original by Pieter Wuille: https://github.com/sipa/bech32. | ||
3698 | * | ||
3699 | * @param {Uint8Array} data Array of integers made up of 'from' bits. | ||
3700 | * @param {number} from Length in bits of elements in the input array. | ||
3701 | * @param {number} to Length in bits of elements in the output array. | ||
3702 | * @param {bool} strictMode Require the conversion to be completed without padding. | ||
3703 | * @returns {Uint8Array} | ||
3704 | */ | ||
3705 | module.exports = function(data, from, to, strictMode) { | ||
3706 | var length = strictMode | ||
3707 | ? Math.floor(data.length * from / to) | ||
3708 | : Math.ceil(data.length * from / to); | ||
3709 | var mask = (1 << to) - 1; | ||
3710 | var result = new Uint8Array(length); | ||
3711 | var index = 0; | ||
3712 | var accumulator = 0; | ||
3713 | var bits = 0; | ||
3714 | for (var i = 0; i < data.length; ++i) { | ||
3715 | var value = data[i]; | ||
3716 | validate(0 <= value && (value >> from) === 0, 'Invalid value: ' + value + '.'); | ||
3717 | accumulator = (accumulator << from) | value; | ||
3718 | bits += from; | ||
3719 | while (bits >= to) { | ||
3720 | bits -= to; | ||
3721 | result[index] = (accumulator >> bits) & mask; | ||
3722 | ++index; | ||
3723 | } | ||
3724 | } | ||
3725 | if (!strictMode) { | ||
3726 | if (bits > 0) { | ||
3727 | result[index] = (accumulator << (to - bits)) & mask; | ||
3728 | ++index; | ||
3729 | } | ||
3730 | } else { | ||
3731 | validate( | ||
3732 | bits < from && ((accumulator << (to - bits)) & mask) === 0, | ||
3733 | 'Input cannot be converted to ' + to + ' bits without padding, but strict mode was used.' | ||
3734 | ); | ||
3735 | } | ||
3736 | return result; | ||
3737 | }; | ||
3738 | |||
3739 | },{"./validation":12}],12:[function(require,module,exports){ | ||
3740 | /** | ||
3741 | * @license | ||
3742 | * https://github.com/bitcoincashjs/cashaddr | ||
3743 | * Copyright (c) 2017-2018 Emilio Almansi | ||
3744 | * Distributed under the MIT software license, see the accompanying | ||
3745 | * file LICENSE or http://www.opensource.org/licenses/mit-license.php. | ||
3746 | */ | ||
3747 | |||
3748 | 'use strict'; | ||
3749 | |||
3750 | /** | ||
3751 | * Validation utility. | ||
3752 | * | ||
3753 | * @module validation | ||
3754 | */ | ||
3755 | |||
3756 | /** | ||
3757 | * Error thrown when encoding or decoding fail due to invalid input. | ||
3758 | * | ||
3759 | * @constructor ValidationError | ||
3760 | * @param {string} message Error description. | ||
3761 | */ | ||
3762 | function ValidationError(message) { | ||
3763 | var error = new Error(); | ||
3764 | this.name = error.name = 'ValidationError'; | ||
3765 | this.message = error.message = message; | ||
3766 | this.stack = error.stack; | ||
3767 | } | ||
3768 | |||
3769 | ValidationError.prototype = Object.create(Error.prototype); | ||
3770 | |||
3771 | /** | ||
3772 | * Validates a given condition, throwing a {@link ValidationError} if | ||
3773 | * the given condition does not hold. | ||
3774 | * | ||
3775 | * @static | ||
3776 | * @param {boolean} condition Condition to validate. | ||
3777 | * @param {string} message Error message in case the condition does not hold. | ||
3778 | */ | ||
3779 | function validate(condition, message) { | ||
3780 | if (!condition) { | ||
3781 | throw new ValidationError(message); | ||
3782 | } | ||
3783 | } | ||
3784 | |||
3785 | module.exports = { | ||
3786 | ValidationError: ValidationError, | ||
3787 | validate: validate, | ||
3788 | }; | ||
3789 | |||
3790 | },{}],13:[function(require,module,exports){ | ||
3791 | var Buffer = require('safe-buffer').Buffer | ||
3792 | var Transform = require('stream').Transform | ||
3793 | var StringDecoder = require('string_decoder').StringDecoder | ||
3794 | var inherits = require('inherits') | ||
3795 | |||
3796 | function CipherBase (hashMode) { | ||
3797 | Transform.call(this) | ||
3798 | this.hashMode = typeof hashMode === 'string' | ||
3799 | if (this.hashMode) { | ||
3800 | this[hashMode] = this._finalOrDigest | ||
3801 | } else { | ||
3802 | this.final = this._finalOrDigest | ||
3803 | } | ||
3804 | if (this._final) { | ||
3805 | this.__final = this._final | ||
3806 | this._final = null | ||
3807 | } | ||
3808 | this._decoder = null | ||
3809 | this._encoding = null | ||
3810 | } | ||
3811 | inherits(CipherBase, Transform) | ||
3812 | |||
3813 | CipherBase.prototype.update = function (data, inputEnc, outputEnc) { | ||
3814 | if (typeof data === 'string') { | ||
3815 | data = Buffer.from(data, inputEnc) | ||
3816 | } | ||
3817 | |||
3818 | var outData = this._update(data) | ||
3819 | if (this.hashMode) return this | ||
3820 | |||
3821 | if (outputEnc) { | ||
3822 | outData = this._toString(outData, outputEnc) | ||
3823 | } | ||
3824 | |||
3825 | return outData | ||
3826 | } | ||
3827 | |||
3828 | CipherBase.prototype.setAutoPadding = function () {} | ||
3829 | CipherBase.prototype.getAuthTag = function () { | ||
3830 | throw new Error('trying to get auth tag in unsupported state') | ||
3831 | } | ||
3832 | |||
3833 | CipherBase.prototype.setAuthTag = function () { | ||
3834 | throw new Error('trying to set auth tag in unsupported state') | ||
3835 | } | ||
3836 | |||
3837 | CipherBase.prototype.setAAD = function () { | ||
3838 | throw new Error('trying to set aad in unsupported state') | ||
3839 | } | ||
3840 | |||
3841 | CipherBase.prototype._transform = function (data, _, next) { | ||
3842 | var err | ||
3843 | try { | ||
3844 | if (this.hashMode) { | ||
3845 | this._update(data) | ||
3846 | } else { | ||
3847 | this.push(this._update(data)) | ||
3848 | } | ||
3849 | } catch (e) { | ||
3850 | err = e | ||
3851 | } finally { | ||
3852 | next(err) | ||
3853 | } | ||
3854 | } | ||
3855 | CipherBase.prototype._flush = function (done) { | ||
3856 | var err | ||
3857 | try { | ||
3858 | this.push(this.__final()) | ||
3859 | } catch (e) { | ||
3860 | err = e | ||
3861 | } | ||
3862 | |||
3863 | done(err) | ||
3864 | } | ||
3865 | CipherBase.prototype._finalOrDigest = function (outputEnc) { | ||
3866 | var outData = this.__final() || Buffer.alloc(0) | ||
3867 | if (outputEnc) { | ||
3868 | outData = this._toString(outData, outputEnc, true) | ||
3869 | } | ||
3870 | return outData | ||
3871 | } | ||
3872 | |||
3873 | CipherBase.prototype._toString = function (value, enc, fin) { | ||
3874 | if (!this._decoder) { | ||
3875 | this._decoder = new StringDecoder(enc) | ||
3876 | this._encoding = enc | ||
3877 | } | ||
3878 | |||
3879 | if (this._encoding !== enc) throw new Error('can\'t switch encodings') | ||
3880 | |||
3881 | var out = this._decoder.write(value) | ||
3882 | if (fin) { | ||
3883 | out += this._decoder.end() | ||
3884 | } | ||
3885 | |||
3886 | return out | ||
3887 | } | ||
3888 | |||
3889 | module.exports = CipherBase | ||
3890 | |||
3891 | },{"inherits":21,"safe-buffer":40,"stream":49,"string_decoder":50}],14:[function(require,module,exports){ | ||
3892 | (function (Buffer){ | ||
3893 | // Copyright Joyent, Inc. and other Node contributors. | ||
3894 | // | ||
3895 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
3896 | // copy of this software and associated documentation files (the | ||
3897 | // "Software"), to deal in the Software without restriction, including | ||
3898 | // without limitation the rights to use, copy, modify, merge, publish, | ||
3899 | // distribute, sublicense, and/or sell copies of the Software, and to permit | ||
3900 | // persons to whom the Software is furnished to do so, subject to the | ||
3901 | // following conditions: | ||
3902 | // | ||
3903 | // The above copyright notice and this permission notice shall be included | ||
3904 | // in all copies or substantial portions of the Software. | ||
3905 | // | ||
3906 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
3907 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
3908 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
3909 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
3910 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
3911 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
3912 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
3913 | |||
3914 | // NOTE: These type checking functions intentionally don't use `instanceof` | ||
3915 | // because it is fragile and can be easily faked with `Object.create()`. | ||
3916 | |||
3917 | function isArray(arg) { | ||
3918 | if (Array.isArray) { | ||
3919 | return Array.isArray(arg); | ||
3920 | } | ||
3921 | return objectToString(arg) === '[object Array]'; | ||
3922 | } | ||
3923 | exports.isArray = isArray; | ||
3924 | |||
3925 | function isBoolean(arg) { | ||
3926 | return typeof arg === 'boolean'; | ||
3927 | } | ||
3928 | exports.isBoolean = isBoolean; | ||
3929 | |||
3930 | function isNull(arg) { | ||
3931 | return arg === null; | ||
3932 | } | ||
3933 | exports.isNull = isNull; | ||
3934 | |||
3935 | function isNullOrUndefined(arg) { | ||
3936 | return arg == null; | ||
3937 | } | ||
3938 | exports.isNullOrUndefined = isNullOrUndefined; | ||
3939 | |||
3940 | function isNumber(arg) { | ||
3941 | return typeof arg === 'number'; | ||
3942 | } | ||
3943 | exports.isNumber = isNumber; | ||
3944 | |||
3945 | function isString(arg) { | ||
3946 | return typeof arg === 'string'; | ||
3947 | } | ||
3948 | exports.isString = isString; | ||
3949 | |||
3950 | function isSymbol(arg) { | ||
3951 | return typeof arg === 'symbol'; | ||
3952 | } | ||
3953 | exports.isSymbol = isSymbol; | ||
3954 | |||
3955 | function isUndefined(arg) { | ||
3956 | return arg === void 0; | ||
3957 | } | ||
3958 | exports.isUndefined = isUndefined; | ||
3959 | |||
3960 | function isRegExp(re) { | ||
3961 | return objectToString(re) === '[object RegExp]'; | ||
3962 | } | ||
3963 | exports.isRegExp = isRegExp; | ||
3964 | |||
3965 | function isObject(arg) { | ||
3966 | return typeof arg === 'object' && arg !== null; | ||
3967 | } | ||
3968 | exports.isObject = isObject; | ||
3969 | |||
3970 | function isDate(d) { | ||
3971 | return objectToString(d) === '[object Date]'; | ||
3972 | } | ||
3973 | exports.isDate = isDate; | ||
3974 | |||
3975 | function isError(e) { | ||
3976 | return (objectToString(e) === '[object Error]' || e instanceof Error); | ||
3977 | } | ||
3978 | exports.isError = isError; | ||
3979 | |||
3980 | function isFunction(arg) { | ||
3981 | return typeof arg === 'function'; | ||
3982 | } | ||
3983 | exports.isFunction = isFunction; | ||
3984 | |||
3985 | function isPrimitive(arg) { | ||
3986 | return arg === null || | ||
3987 | typeof arg === 'boolean' || | ||
3988 | typeof arg === 'number' || | ||
3989 | typeof arg === 'string' || | ||
3990 | typeof arg === 'symbol' || // ES6 symbol | ||
3991 | typeof arg === 'undefined'; | ||
3992 | } | ||
3993 | exports.isPrimitive = isPrimitive; | ||
3994 | |||
3995 | exports.isBuffer = Buffer.isBuffer; | ||
3996 | |||
3997 | function objectToString(o) { | ||
3998 | return Object.prototype.toString.call(o); | ||
3999 | } | ||
4000 | |||
4001 | }).call(this,{"isBuffer":require("../../is-buffer/index.js")}) | ||
4002 | },{"../../is-buffer/index.js":22}],15:[function(require,module,exports){ | ||
4003 | (function (Buffer){ | ||
4004 | 'use strict' | ||
4005 | var inherits = require('inherits') | ||
4006 | var md5 = require('./md5') | ||
4007 | var RIPEMD160 = require('ripemd160') | ||
4008 | var sha = require('sha.js') | ||
4009 | |||
4010 | var Base = require('cipher-base') | ||
4011 | |||
4012 | function HashNoConstructor (hash) { | ||
4013 | Base.call(this, 'digest') | ||
4014 | |||
4015 | this._hash = hash | ||
4016 | this.buffers = [] | ||
4017 | } | ||
4018 | |||
4019 | inherits(HashNoConstructor, Base) | ||
4020 | |||
4021 | HashNoConstructor.prototype._update = function (data) { | ||
4022 | this.buffers.push(data) | ||
4023 | } | ||
4024 | |||
4025 | HashNoConstructor.prototype._final = function () { | ||
4026 | var buf = Buffer.concat(this.buffers) | ||
4027 | var r = this._hash(buf) | ||
4028 | this.buffers = null | ||
4029 | |||
4030 | return r | ||
4031 | } | ||
4032 | |||
4033 | function Hash (hash) { | ||
4034 | Base.call(this, 'digest') | ||
4035 | |||
4036 | this._hash = hash | ||
4037 | } | ||
4038 | |||
4039 | inherits(Hash, Base) | ||
4040 | |||
4041 | Hash.prototype._update = function (data) { | ||
4042 | this._hash.update(data) | ||
4043 | } | ||
4044 | |||
4045 | Hash.prototype._final = function () { | ||
4046 | return this._hash.digest() | ||
4047 | } | ||
4048 | |||
4049 | module.exports = function createHash (alg) { | ||
4050 | alg = alg.toLowerCase() | ||
4051 | if (alg === 'md5') return new HashNoConstructor(md5) | ||
4052 | if (alg === 'rmd160' || alg === 'ripemd160') return new Hash(new RIPEMD160()) | ||
4053 | |||
4054 | return new Hash(sha(alg)) | ||
4055 | } | ||
4056 | |||
4057 | }).call(this,require("buffer").Buffer) | ||
4058 | },{"./md5":17,"buffer":8,"cipher-base":13,"inherits":21,"ripemd160":39,"sha.js":42}],16:[function(require,module,exports){ | ||
4059 | (function (Buffer){ | ||
4060 | 'use strict' | ||
4061 | var intSize = 4 | ||
4062 | var zeroBuffer = new Buffer(intSize) | ||
4063 | zeroBuffer.fill(0) | ||
4064 | |||
4065 | var charSize = 8 | ||
4066 | var hashSize = 16 | ||
4067 | |||
4068 | function toArray (buf) { | ||
4069 | if ((buf.length % intSize) !== 0) { | ||
4070 | var len = buf.length + (intSize - (buf.length % intSize)) | ||
4071 | buf = Buffer.concat([buf, zeroBuffer], len) | ||
4072 | } | ||
4073 | |||
4074 | var arr = new Array(buf.length >>> 2) | ||
4075 | for (var i = 0, j = 0; i < buf.length; i += intSize, j++) { | ||
4076 | arr[j] = buf.readInt32LE(i) | ||
4077 | } | ||
4078 | |||
4079 | return arr | ||
4080 | } | ||
4081 | |||
4082 | module.exports = function hash (buf, fn) { | ||
4083 | var arr = fn(toArray(buf), buf.length * charSize) | ||
4084 | buf = new Buffer(hashSize) | ||
4085 | for (var i = 0; i < arr.length; i++) { | ||
4086 | buf.writeInt32LE(arr[i], i << 2, true) | ||
4087 | } | ||
4088 | return buf | ||
4089 | } | ||
4090 | |||
4091 | }).call(this,require("buffer").Buffer) | ||
4092 | },{"buffer":8}],17:[function(require,module,exports){ | ||
4093 | 'use strict' | ||
4094 | /* | ||
4095 | * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message | ||
4096 | * Digest Algorithm, as defined in RFC 1321. | ||
4097 | * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. | ||
4098 | * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet | ||
4099 | * Distributed under the BSD License | ||
4100 | * See http://pajhome.org.uk/crypt/md5 for more info. | ||
4101 | */ | ||
4102 | |||
4103 | var makeHash = require('./make-hash') | ||
4104 | |||
4105 | /* | ||
4106 | * Calculate the MD5 of an array of little-endian words, and a bit length | ||
4107 | */ | ||
4108 | function core_md5 (x, len) { | ||
4109 | /* append padding */ | ||
4110 | x[len >> 5] |= 0x80 << ((len) % 32) | ||
4111 | x[(((len + 64) >>> 9) << 4) + 14] = len | ||
4112 | |||
4113 | var a = 1732584193 | ||
4114 | var b = -271733879 | ||
4115 | var c = -1732584194 | ||
4116 | var d = 271733878 | ||
4117 | |||
4118 | for (var i = 0; i < x.length; i += 16) { | ||
4119 | var olda = a | ||
4120 | var oldb = b | ||
4121 | var oldc = c | ||
4122 | var oldd = d | ||
4123 | |||
4124 | a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936) | ||
4125 | d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586) | ||
4126 | c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819) | ||
4127 | b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330) | ||
4128 | a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897) | ||
4129 | d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426) | ||
4130 | c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341) | ||
4131 | b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983) | ||
4132 | a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416) | ||
4133 | d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417) | ||
4134 | c = md5_ff(c, d, a, b, x[i + 10], 17, -42063) | ||
4135 | b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162) | ||
4136 | a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682) | ||
4137 | d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101) | ||
4138 | c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290) | ||
4139 | b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329) | ||
4140 | |||
4141 | a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510) | ||
4142 | d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632) | ||
4143 | c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713) | ||
4144 | b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302) | ||
4145 | a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691) | ||
4146 | d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083) | ||
4147 | c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335) | ||
4148 | b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848) | ||
4149 | a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438) | ||
4150 | d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690) | ||
4151 | c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961) | ||
4152 | b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501) | ||
4153 | a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467) | ||
4154 | d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784) | ||
4155 | c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473) | ||
4156 | b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734) | ||
4157 | |||
4158 | a = md5_hh(a, b, c, d, x[i + 5], 4, -378558) | ||
4159 | d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463) | ||
4160 | c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562) | ||
4161 | b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556) | ||
4162 | a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060) | ||
4163 | d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353) | ||
4164 | c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632) | ||
4165 | b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640) | ||
4166 | a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174) | ||
4167 | d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222) | ||
4168 | c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979) | ||
4169 | b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189) | ||
4170 | a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487) | ||
4171 | d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835) | ||
4172 | c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520) | ||
4173 | b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651) | ||
4174 | |||
4175 | a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844) | ||
4176 | d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415) | ||
4177 | c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905) | ||
4178 | b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055) | ||
4179 | a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571) | ||
4180 | d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606) | ||
4181 | c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523) | ||
4182 | b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799) | ||
4183 | a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359) | ||
4184 | d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744) | ||
4185 | c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380) | ||
4186 | b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649) | ||
4187 | a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070) | ||
4188 | d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379) | ||
4189 | c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259) | ||
4190 | b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551) | ||
4191 | |||
4192 | a = safe_add(a, olda) | ||
4193 | b = safe_add(b, oldb) | ||
4194 | c = safe_add(c, oldc) | ||
4195 | d = safe_add(d, oldd) | ||
4196 | } | ||
4197 | |||
4198 | return [a, b, c, d] | ||
4199 | } | ||
4200 | |||
4201 | /* | ||
4202 | * These functions implement the four basic operations the algorithm uses. | ||
4203 | */ | ||
4204 | function md5_cmn (q, a, b, x, s, t) { | ||
4205 | return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b) | ||
4206 | } | ||
4207 | |||
4208 | function md5_ff (a, b, c, d, x, s, t) { | ||
4209 | return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t) | ||
4210 | } | ||
4211 | |||
4212 | function md5_gg (a, b, c, d, x, s, t) { | ||
4213 | return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t) | ||
4214 | } | ||
4215 | |||
4216 | function md5_hh (a, b, c, d, x, s, t) { | ||
4217 | return md5_cmn(b ^ c ^ d, a, b, x, s, t) | ||
4218 | } | ||
4219 | |||
4220 | function md5_ii (a, b, c, d, x, s, t) { | ||
4221 | return md5_cmn(c ^ (b | (~d)), a, b, x, s, t) | ||
4222 | } | ||
4223 | |||
4224 | /* | ||
4225 | * Add integers, wrapping at 2^32. This uses 16-bit operations internally | ||
4226 | * to work around bugs in some JS interpreters. | ||
4227 | */ | ||
4228 | function safe_add (x, y) { | ||
4229 | var lsw = (x & 0xFFFF) + (y & 0xFFFF) | ||
4230 | var msw = (x >> 16) + (y >> 16) + (lsw >> 16) | ||
4231 | return (msw << 16) | (lsw & 0xFFFF) | ||
4232 | } | ||
4233 | |||
4234 | /* | ||
4235 | * Bitwise rotate a 32-bit number to the left. | ||
4236 | */ | ||
4237 | function bit_rol (num, cnt) { | ||
4238 | return (num << cnt) | (num >>> (32 - cnt)) | ||
4239 | } | ||
4240 | |||
4241 | module.exports = function md5 (buf) { | ||
4242 | return makeHash(buf, core_md5) | ||
4243 | } | ||
4244 | |||
4245 | },{"./make-hash":16}],18:[function(require,module,exports){ | ||
4246 | // Copyright Joyent, Inc. and other Node contributors. | ||
4247 | // | ||
4248 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
4249 | // copy of this software and associated documentation files (the | ||
4250 | // "Software"), to deal in the Software without restriction, including | ||
4251 | // without limitation the rights to use, copy, modify, merge, publish, | ||
4252 | // distribute, sublicense, and/or sell copies of the Software, and to permit | ||
4253 | // persons to whom the Software is furnished to do so, subject to the | ||
4254 | // following conditions: | ||
4255 | // | ||
4256 | // The above copyright notice and this permission notice shall be included | ||
4257 | // in all copies or substantial portions of the Software. | ||
4258 | // | ||
4259 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
4260 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
4261 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
4262 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
4263 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
4264 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
4265 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
4266 | |||
4267 | function EventEmitter() { | ||
4268 | this._events = this._events || {}; | ||
4269 | this._maxListeners = this._maxListeners || undefined; | ||
4270 | } | ||
4271 | module.exports = EventEmitter; | ||
4272 | |||
4273 | // Backwards-compat with node 0.10.x | ||
4274 | EventEmitter.EventEmitter = EventEmitter; | ||
4275 | |||
4276 | EventEmitter.prototype._events = undefined; | ||
4277 | EventEmitter.prototype._maxListeners = undefined; | ||
4278 | |||
4279 | // By default EventEmitters will print a warning if more than 10 listeners are | ||
4280 | // added to it. This is a useful default which helps finding memory leaks. | ||
4281 | EventEmitter.defaultMaxListeners = 10; | ||
4282 | |||
4283 | // Obviously not all Emitters should be limited to 10. This function allows | ||
4284 | // that to be increased. Set to zero for unlimited. | ||
4285 | EventEmitter.prototype.setMaxListeners = function(n) { | ||
4286 | if (!isNumber(n) || n < 0 || isNaN(n)) | ||
4287 | throw TypeError('n must be a positive number'); | ||
4288 | this._maxListeners = n; | ||
4289 | return this; | ||
4290 | }; | ||
4291 | |||
4292 | EventEmitter.prototype.emit = function(type) { | ||
4293 | var er, handler, len, args, i, listeners; | ||
4294 | |||
4295 | if (!this._events) | ||
4296 | this._events = {}; | ||
4297 | |||
4298 | // If there is no 'error' event listener then throw. | ||
4299 | if (type === 'error') { | ||
4300 | if (!this._events.error || | ||
4301 | (isObject(this._events.error) && !this._events.error.length)) { | ||
4302 | er = arguments[1]; | ||
4303 | if (er instanceof Error) { | ||
4304 | throw er; // Unhandled 'error' event | ||
4305 | } else { | ||
4306 | // At least give some kind of context to the user | ||
4307 | var err = new Error('Uncaught, unspecified "error" event. (' + er + ')'); | ||
4308 | err.context = er; | ||
4309 | throw err; | ||
4310 | } | ||
4311 | } | ||
4312 | } | ||
4313 | |||
4314 | handler = this._events[type]; | ||
4315 | |||
4316 | if (isUndefined(handler)) | ||
4317 | return false; | ||
4318 | |||
4319 | if (isFunction(handler)) { | ||
4320 | switch (arguments.length) { | ||
4321 | // fast cases | ||
4322 | case 1: | ||
4323 | handler.call(this); | ||
4324 | break; | ||
4325 | case 2: | ||
4326 | handler.call(this, arguments[1]); | ||
4327 | break; | ||
4328 | case 3: | ||
4329 | handler.call(this, arguments[1], arguments[2]); | ||
4330 | break; | ||
4331 | // slower | ||
4332 | default: | ||
4333 | args = Array.prototype.slice.call(arguments, 1); | ||
4334 | handler.apply(this, args); | ||
4335 | } | ||
4336 | } else if (isObject(handler)) { | ||
4337 | args = Array.prototype.slice.call(arguments, 1); | ||
4338 | listeners = handler.slice(); | ||
4339 | len = listeners.length; | ||
4340 | for (i = 0; i < len; i++) | ||
4341 | listeners[i].apply(this, args); | ||
4342 | } | ||
4343 | |||
4344 | return true; | ||
4345 | }; | ||
4346 | |||
4347 | EventEmitter.prototype.addListener = function(type, listener) { | ||
4348 | var m; | ||
4349 | |||
4350 | if (!isFunction(listener)) | ||
4351 | throw TypeError('listener must be a function'); | ||
4352 | |||
4353 | if (!this._events) | ||
4354 | this._events = {}; | ||
4355 | |||
4356 | // To avoid recursion in the case that type === "newListener"! Before | ||
4357 | // adding it to the listeners, first emit "newListener". | ||
4358 | if (this._events.newListener) | ||
4359 | this.emit('newListener', type, | ||
4360 | isFunction(listener.listener) ? | ||
4361 | listener.listener : listener); | ||
4362 | |||
4363 | if (!this._events[type]) | ||
4364 | // Optimize the case of one listener. Don't need the extra array object. | ||
4365 | this._events[type] = listener; | ||
4366 | else if (isObject(this._events[type])) | ||
4367 | // If we've already got an array, just append. | ||
4368 | this._events[type].push(listener); | ||
4369 | else | ||
4370 | // Adding the second element, need to change to array. | ||
4371 | this._events[type] = [this._events[type], listener]; | ||
4372 | |||
4373 | // Check for listener leak | ||
4374 | if (isObject(this._events[type]) && !this._events[type].warned) { | ||
4375 | if (!isUndefined(this._maxListeners)) { | ||
4376 | m = this._maxListeners; | ||
4377 | } else { | ||
4378 | m = EventEmitter.defaultMaxListeners; | ||
4379 | } | ||
4380 | |||
4381 | if (m && m > 0 && this._events[type].length > m) { | ||
4382 | this._events[type].warned = true; | ||
4383 | console.error('(node) warning: possible EventEmitter memory ' + | ||
4384 | 'leak detected. %d listeners added. ' + | ||
4385 | 'Use emitter.setMaxListeners() to increase limit.', | ||
4386 | this._events[type].length); | ||
4387 | if (typeof console.trace === 'function') { | ||
4388 | // not supported in IE 10 | ||
4389 | console.trace(); | ||
4390 | } | ||
4391 | } | ||
4392 | } | ||
4393 | |||
4394 | return this; | ||
4395 | }; | ||
4396 | |||
4397 | EventEmitter.prototype.on = EventEmitter.prototype.addListener; | ||
4398 | |||
4399 | EventEmitter.prototype.once = function(type, listener) { | ||
4400 | if (!isFunction(listener)) | ||
4401 | throw TypeError('listener must be a function'); | ||
4402 | |||
4403 | var fired = false; | ||
4404 | |||
4405 | function g() { | ||
4406 | this.removeListener(type, g); | ||
4407 | |||
4408 | if (!fired) { | ||
4409 | fired = true; | ||
4410 | listener.apply(this, arguments); | ||
4411 | } | ||
4412 | } | ||
4413 | |||
4414 | g.listener = listener; | ||
4415 | this.on(type, g); | ||
4416 | |||
4417 | return this; | ||
4418 | }; | ||
4419 | |||
4420 | // emits a 'removeListener' event iff the listener was removed | ||
4421 | EventEmitter.prototype.removeListener = function(type, listener) { | ||
4422 | var list, position, length, i; | ||
4423 | |||
4424 | if (!isFunction(listener)) | ||
4425 | throw TypeError('listener must be a function'); | ||
4426 | |||
4427 | if (!this._events || !this._events[type]) | ||
4428 | return this; | ||
4429 | |||
4430 | list = this._events[type]; | ||
4431 | length = list.length; | ||
4432 | position = -1; | ||
4433 | |||
4434 | if (list === listener || | ||
4435 | (isFunction(list.listener) && list.listener === listener)) { | ||
4436 | delete this._events[type]; | ||
4437 | if (this._events.removeListener) | ||
4438 | this.emit('removeListener', type, listener); | ||
4439 | |||
4440 | } else if (isObject(list)) { | ||
4441 | for (i = length; i-- > 0;) { | ||
4442 | if (list[i] === listener || | ||
4443 | (list[i].listener && list[i].listener === listener)) { | ||
4444 | position = i; | ||
4445 | break; | ||
4446 | } | ||
4447 | } | ||
4448 | |||
4449 | if (position < 0) | ||
4450 | return this; | ||
4451 | |||
4452 | if (list.length === 1) { | ||
4453 | list.length = 0; | ||
4454 | delete this._events[type]; | ||
4455 | } else { | ||
4456 | list.splice(position, 1); | ||
4457 | } | ||
4458 | |||
4459 | if (this._events.removeListener) | ||
4460 | this.emit('removeListener', type, listener); | ||
4461 | } | ||
4462 | |||
4463 | return this; | ||
4464 | }; | ||
4465 | |||
4466 | EventEmitter.prototype.removeAllListeners = function(type) { | ||
4467 | var key, listeners; | ||
4468 | |||
4469 | if (!this._events) | ||
4470 | return this; | ||
4471 | |||
4472 | // not listening for removeListener, no need to emit | ||
4473 | if (!this._events.removeListener) { | ||
4474 | if (arguments.length === 0) | ||
4475 | this._events = {}; | ||
4476 | else if (this._events[type]) | ||
4477 | delete this._events[type]; | ||
4478 | return this; | ||
4479 | } | ||
4480 | |||
4481 | // emit removeListener for all listeners on all events | ||
4482 | if (arguments.length === 0) { | ||
4483 | for (key in this._events) { | ||
4484 | if (key === 'removeListener') continue; | ||
4485 | this.removeAllListeners(key); | ||
4486 | } | ||
4487 | this.removeAllListeners('removeListener'); | ||
4488 | this._events = {}; | ||
4489 | return this; | ||
4490 | } | ||
4491 | |||
4492 | listeners = this._events[type]; | ||
4493 | |||
4494 | if (isFunction(listeners)) { | ||
4495 | this.removeListener(type, listeners); | ||
4496 | } else if (listeners) { | ||
4497 | // LIFO order | ||
4498 | while (listeners.length) | ||
4499 | this.removeListener(type, listeners[listeners.length - 1]); | ||
4500 | } | ||
4501 | delete this._events[type]; | ||
4502 | |||
4503 | return this; | ||
4504 | }; | ||
4505 | |||
4506 | EventEmitter.prototype.listeners = function(type) { | ||
4507 | var ret; | ||
4508 | if (!this._events || !this._events[type]) | ||
4509 | ret = []; | ||
4510 | else if (isFunction(this._events[type])) | ||
4511 | ret = [this._events[type]]; | ||
4512 | else | ||
4513 | ret = this._events[type].slice(); | ||
4514 | return ret; | ||
4515 | }; | ||
4516 | |||
4517 | EventEmitter.prototype.listenerCount = function(type) { | ||
4518 | if (this._events) { | ||
4519 | var evlistener = this._events[type]; | ||
4520 | |||
4521 | if (isFunction(evlistener)) | ||
4522 | return 1; | ||
4523 | else if (evlistener) | ||
4524 | return evlistener.length; | ||
4525 | } | ||
4526 | return 0; | ||
4527 | }; | ||
4528 | |||
4529 | EventEmitter.listenerCount = function(emitter, type) { | ||
4530 | return emitter.listenerCount(type); | ||
4531 | }; | ||
4532 | |||
4533 | function isFunction(arg) { | ||
4534 | return typeof arg === 'function'; | ||
4535 | } | ||
4536 | |||
4537 | function isNumber(arg) { | ||
4538 | return typeof arg === 'number'; | ||
4539 | } | ||
4540 | |||
4541 | function isObject(arg) { | ||
4542 | return typeof arg === 'object' && arg !== null; | ||
4543 | } | ||
4544 | |||
4545 | function isUndefined(arg) { | ||
4546 | return arg === void 0; | ||
4547 | } | ||
4548 | |||
4549 | },{}],19:[function(require,module,exports){ | ||
4550 | (function (Buffer){ | ||
4551 | 'use strict' | ||
4552 | var Transform = require('stream').Transform | ||
4553 | var inherits = require('inherits') | ||
4554 | |||
4555 | function HashBase (blockSize) { | ||
4556 | Transform.call(this) | ||
4557 | |||
4558 | this._block = new Buffer(blockSize) | ||
4559 | this._blockSize = blockSize | ||
4560 | this._blockOffset = 0 | ||
4561 | this._length = [0, 0, 0, 0] | ||
4562 | |||
4563 | this._finalized = false | ||
4564 | } | ||
4565 | |||
4566 | inherits(HashBase, Transform) | ||
4567 | |||
4568 | HashBase.prototype._transform = function (chunk, encoding, callback) { | ||
4569 | var error = null | ||
4570 | try { | ||
4571 | if (encoding !== 'buffer') chunk = new Buffer(chunk, encoding) | ||
4572 | this.update(chunk) | ||
4573 | } catch (err) { | ||
4574 | error = err | ||
4575 | } | ||
4576 | |||
4577 | callback(error) | ||
4578 | } | ||
4579 | |||
4580 | HashBase.prototype._flush = function (callback) { | ||
4581 | var error = null | ||
4582 | try { | ||
4583 | this.push(this._digest()) | ||
4584 | } catch (err) { | ||
4585 | error = err | ||
4586 | } | ||
4587 | |||
4588 | callback(error) | ||
4589 | } | ||
4590 | |||
4591 | HashBase.prototype.update = function (data, encoding) { | ||
4592 | if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer') | ||
4593 | if (this._finalized) throw new Error('Digest already called') | ||
4594 | if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding || 'binary') | ||
4595 | |||
4596 | // consume data | ||
4597 | var block = this._block | ||
4598 | var offset = 0 | ||
4599 | while (this._blockOffset + data.length - offset >= this._blockSize) { | ||
4600 | for (var i = this._blockOffset; i < this._blockSize;) block[i++] = data[offset++] | ||
4601 | this._update() | ||
4602 | this._blockOffset = 0 | ||
4603 | } | ||
4604 | while (offset < data.length) block[this._blockOffset++] = data[offset++] | ||
4605 | |||
4606 | // update length | ||
4607 | for (var j = 0, carry = data.length * 8; carry > 0; ++j) { | ||
4608 | this._length[j] += carry | ||
4609 | carry = (this._length[j] / 0x0100000000) | 0 | ||
4610 | if (carry > 0) this._length[j] -= 0x0100000000 * carry | ||
4611 | } | ||
4612 | |||
4613 | return this | ||
4614 | } | ||
4615 | |||
4616 | HashBase.prototype._update = function (data) { | ||
4617 | throw new Error('_update is not implemented') | ||
4618 | } | ||
4619 | |||
4620 | HashBase.prototype.digest = function (encoding) { | ||
4621 | if (this._finalized) throw new Error('Digest already called') | ||
4622 | this._finalized = true | ||
4623 | |||
4624 | var digest = this._digest() | ||
4625 | if (encoding !== undefined) digest = digest.toString(encoding) | ||
4626 | return digest | ||
4627 | } | ||
4628 | |||
4629 | HashBase.prototype._digest = function () { | ||
4630 | throw new Error('_digest is not implemented') | ||
4631 | } | ||
4632 | |||
4633 | module.exports = HashBase | ||
4634 | |||
4635 | }).call(this,require("buffer").Buffer) | ||
4636 | },{"buffer":8,"inherits":21,"stream":49}],20:[function(require,module,exports){ | ||
4637 | exports.read = function (buffer, offset, isLE, mLen, nBytes) { | ||
4638 | var e, m | ||
4639 | var eLen = nBytes * 8 - mLen - 1 | ||
4640 | var eMax = (1 << eLen) - 1 | ||
4641 | var eBias = eMax >> 1 | ||
4642 | var nBits = -7 | ||
4643 | var i = isLE ? (nBytes - 1) : 0 | ||
4644 | var d = isLE ? -1 : 1 | ||
4645 | var s = buffer[offset + i] | ||
4646 | |||
4647 | i += d | ||
4648 | |||
4649 | e = s & ((1 << (-nBits)) - 1) | ||
4650 | s >>= (-nBits) | ||
4651 | nBits += eLen | ||
4652 | for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {} | ||
4653 | |||
4654 | m = e & ((1 << (-nBits)) - 1) | ||
4655 | e >>= (-nBits) | ||
4656 | nBits += mLen | ||
4657 | for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {} | ||
4658 | |||
4659 | if (e === 0) { | ||
4660 | e = 1 - eBias | ||
4661 | } else if (e === eMax) { | ||
4662 | return m ? NaN : ((s ? -1 : 1) * Infinity) | ||
4663 | } else { | ||
4664 | m = m + Math.pow(2, mLen) | ||
4665 | e = e - eBias | ||
4666 | } | ||
4667 | return (s ? -1 : 1) * m * Math.pow(2, e - mLen) | ||
4668 | } | ||
4669 | |||
4670 | exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { | ||
4671 | var e, m, c | ||
4672 | var eLen = nBytes * 8 - mLen - 1 | ||
4673 | var eMax = (1 << eLen) - 1 | ||
4674 | var eBias = eMax >> 1 | ||
4675 | var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) | ||
4676 | var i = isLE ? 0 : (nBytes - 1) | ||
4677 | var d = isLE ? 1 : -1 | ||
4678 | var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0 | ||
4679 | |||
4680 | value = Math.abs(value) | ||
4681 | |||
4682 | if (isNaN(value) || value === Infinity) { | ||
4683 | m = isNaN(value) ? 1 : 0 | ||
4684 | e = eMax | ||
4685 | } else { | ||
4686 | e = Math.floor(Math.log(value) / Math.LN2) | ||
4687 | if (value * (c = Math.pow(2, -e)) < 1) { | ||
4688 | e-- | ||
4689 | c *= 2 | ||
4690 | } | ||
4691 | if (e + eBias >= 1) { | ||
4692 | value += rt / c | ||
4693 | } else { | ||
4694 | value += rt * Math.pow(2, 1 - eBias) | ||
4695 | } | ||
4696 | if (value * c >= 2) { | ||
4697 | e++ | ||
4698 | c /= 2 | ||
4699 | } | ||
4700 | |||
4701 | if (e + eBias >= eMax) { | ||
4702 | m = 0 | ||
4703 | e = eMax | ||
4704 | } else if (e + eBias >= 1) { | ||
4705 | m = (value * c - 1) * Math.pow(2, mLen) | ||
4706 | e = e + eBias | ||
4707 | } else { | ||
4708 | m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) | ||
4709 | e = 0 | ||
4710 | } | ||
4711 | } | ||
4712 | |||
4713 | for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} | ||
4714 | |||
4715 | e = (e << mLen) | m | ||
4716 | eLen += mLen | ||
4717 | for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} | ||
4718 | |||
4719 | buffer[offset + i - d] |= s * 128 | ||
4720 | } | ||
4721 | |||
4722 | },{}],21:[function(require,module,exports){ | ||
4723 | if (typeof Object.create === 'function') { | ||
4724 | // implementation from standard node.js 'util' module | ||
4725 | module.exports = function inherits(ctor, superCtor) { | ||
4726 | ctor.super_ = superCtor | ||
4727 | ctor.prototype = Object.create(superCtor.prototype, { | ||
4728 | constructor: { | ||
4729 | value: ctor, | ||
4730 | enumerable: false, | ||
4731 | writable: true, | ||
4732 | configurable: true | ||
4733 | } | ||
4734 | }); | ||
4735 | }; | ||
4736 | } else { | ||
4737 | // old school shim for old browsers | ||
4738 | module.exports = function inherits(ctor, superCtor) { | ||
4739 | ctor.super_ = superCtor | ||
4740 | var TempCtor = function () {} | ||
4741 | TempCtor.prototype = superCtor.prototype | ||
4742 | ctor.prototype = new TempCtor() | ||
4743 | ctor.prototype.constructor = ctor | ||
4744 | } | ||
4745 | } | ||
4746 | |||
4747 | },{}],22:[function(require,module,exports){ | ||
4748 | /*! | ||
4749 | * Determine if an object is a Buffer | ||
4750 | * | ||
4751 | * @author Feross Aboukhadijeh <https://feross.org> | ||
4752 | * @license MIT | ||
4753 | */ | ||
4754 | |||
4755 | // The _isBuffer check is for Safari 5-7 support, because it's missing | ||
4756 | // Object.prototype.constructor. Remove this eventually | ||
4757 | module.exports = function (obj) { | ||
4758 | return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer) | ||
4759 | } | ||
4760 | |||
4761 | function isBuffer (obj) { | ||
4762 | return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj) | ||
4763 | } | ||
4764 | |||
4765 | // For Node v0.10 support. Remove this eventually. | ||
4766 | function isSlowBuffer (obj) { | ||
4767 | return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0)) | ||
4768 | } | ||
4769 | |||
4770 | },{}],23:[function(require,module,exports){ | ||
4771 | var toString = {}.toString; | ||
4772 | |||
4773 | module.exports = Array.isArray || function (arr) { | ||
4774 | return toString.call(arr) == '[object Array]'; | ||
4775 | }; | ||
4776 | |||
4777 | },{}],24:[function(require,module,exports){ | ||
4778 | (function (process){ | ||
4779 | 'use strict'; | ||
4780 | |||
4781 | if (!process.version || | ||
4782 | process.version.indexOf('v0.') === 0 || | ||
4783 | process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) { | ||
4784 | module.exports = nextTick; | ||
4785 | } else { | ||
4786 | module.exports = process.nextTick; | ||
4787 | } | ||
4788 | |||
4789 | function nextTick(fn, arg1, arg2, arg3) { | ||
4790 | if (typeof fn !== 'function') { | ||
4791 | throw new TypeError('"callback" argument must be a function'); | ||
4792 | } | ||
4793 | var len = arguments.length; | ||
4794 | var args, i; | ||
4795 | switch (len) { | ||
4796 | case 0: | ||
4797 | case 1: | ||
4798 | return process.nextTick(fn); | ||
4799 | case 2: | ||
4800 | return process.nextTick(function afterTickOne() { | ||
4801 | fn.call(null, arg1); | ||
4802 | }); | ||
4803 | case 3: | ||
4804 | return process.nextTick(function afterTickTwo() { | ||
4805 | fn.call(null, arg1, arg2); | ||
4806 | }); | ||
4807 | case 4: | ||
4808 | return process.nextTick(function afterTickThree() { | ||
4809 | fn.call(null, arg1, arg2, arg3); | ||
4810 | }); | ||
4811 | default: | ||
4812 | args = new Array(len - 1); | ||
4813 | i = 0; | ||
4814 | while (i < args.length) { | ||
4815 | args[i++] = arguments[i]; | ||
4816 | } | ||
4817 | return process.nextTick(function afterTick() { | ||
4818 | fn.apply(null, args); | ||
4819 | }); | ||
4820 | } | ||
4821 | } | ||
4822 | |||
4823 | }).call(this,require('_process')) | ||
4824 | },{"_process":25}],25:[function(require,module,exports){ | ||
4825 | // shim for using process in browser | ||
4826 | var process = module.exports = {}; | ||
4827 | |||
4828 | // cached from whatever global is present so that test runners that stub it | ||
4829 | // don't break things. But we need to wrap it in a try catch in case it is | ||
4830 | // wrapped in strict mode code which doesn't define any globals. It's inside a | ||
4831 | // function because try/catches deoptimize in certain engines. | ||
4832 | |||
4833 | var cachedSetTimeout; | ||
4834 | var cachedClearTimeout; | ||
4835 | |||
4836 | function defaultSetTimout() { | ||
4837 | throw new Error('setTimeout has not been defined'); | ||
4838 | } | ||
4839 | function defaultClearTimeout () { | ||
4840 | throw new Error('clearTimeout has not been defined'); | ||
4841 | } | ||
4842 | (function () { | ||
4843 | try { | ||
4844 | if (typeof setTimeout === 'function') { | ||
4845 | cachedSetTimeout = setTimeout; | ||
4846 | } else { | ||
4847 | cachedSetTimeout = defaultSetTimout; | ||
4848 | } | ||
4849 | } catch (e) { | ||
4850 | cachedSetTimeout = defaultSetTimout; | ||
4851 | } | ||
4852 | try { | ||
4853 | if (typeof clearTimeout === 'function') { | ||
4854 | cachedClearTimeout = clearTimeout; | ||
4855 | } else { | ||
4856 | cachedClearTimeout = defaultClearTimeout; | ||
4857 | } | ||
4858 | } catch (e) { | ||
4859 | cachedClearTimeout = defaultClearTimeout; | ||
4860 | } | ||
4861 | } ()) | ||
4862 | function runTimeout(fun) { | ||
4863 | if (cachedSetTimeout === setTimeout) { | ||
4864 | //normal enviroments in sane situations | ||
4865 | return setTimeout(fun, 0); | ||
4866 | } | ||
4867 | // if setTimeout wasn't available but was latter defined | ||
4868 | if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { | ||
4869 | cachedSetTimeout = setTimeout; | ||
4870 | return setTimeout(fun, 0); | ||
4871 | } | ||
4872 | try { | ||
4873 | // when when somebody has screwed with setTimeout but no I.E. maddness | ||
4874 | return cachedSetTimeout(fun, 0); | ||
4875 | } catch(e){ | ||
4876 | try { | ||
4877 | // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally | ||
4878 | return cachedSetTimeout.call(null, fun, 0); | ||
4879 | } catch(e){ | ||
4880 | // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error | ||
4881 | return cachedSetTimeout.call(this, fun, 0); | ||
4882 | } | ||
4883 | } | ||
4884 | |||
4885 | |||
4886 | } | ||
4887 | function runClearTimeout(marker) { | ||
4888 | if (cachedClearTimeout === clearTimeout) { | ||
4889 | //normal enviroments in sane situations | ||
4890 | return clearTimeout(marker); | ||
4891 | } | ||
4892 | // if clearTimeout wasn't available but was latter defined | ||
4893 | if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { | ||
4894 | cachedClearTimeout = clearTimeout; | ||
4895 | return clearTimeout(marker); | ||
4896 | } | ||
4897 | try { | ||
4898 | // when when somebody has screwed with setTimeout but no I.E. maddness | ||
4899 | return cachedClearTimeout(marker); | ||
4900 | } catch (e){ | ||
4901 | try { | ||
4902 | // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally | ||
4903 | return cachedClearTimeout.call(null, marker); | ||
4904 | } catch (e){ | ||
4905 | // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error. | ||
4906 | // Some versions of I.E. have different rules for clearTimeout vs setTimeout | ||
4907 | return cachedClearTimeout.call(this, marker); | ||
4908 | } | ||
4909 | } | ||
4910 | |||
4911 | |||
4912 | |||
4913 | } | ||
4914 | var queue = []; | ||
4915 | var draining = false; | ||
4916 | var currentQueue; | ||
4917 | var queueIndex = -1; | ||
4918 | |||
4919 | function cleanUpNextTick() { | ||
4920 | if (!draining || !currentQueue) { | ||
4921 | return; | ||
4922 | } | ||
4923 | draining = false; | ||
4924 | if (currentQueue.length) { | ||
4925 | queue = currentQueue.concat(queue); | ||
4926 | } else { | ||
4927 | queueIndex = -1; | ||
4928 | } | ||
4929 | if (queue.length) { | ||
4930 | drainQueue(); | ||
4931 | } | ||
4932 | } | ||
4933 | |||
4934 | function drainQueue() { | ||
4935 | if (draining) { | ||
4936 | return; | ||
4937 | } | ||
4938 | var timeout = runTimeout(cleanUpNextTick); | ||
4939 | draining = true; | ||
4940 | |||
4941 | var len = queue.length; | ||
4942 | while(len) { | ||
4943 | currentQueue = queue; | ||
4944 | queue = []; | ||
4945 | while (++queueIndex < len) { | ||
4946 | if (currentQueue) { | ||
4947 | currentQueue[queueIndex].run(); | ||
4948 | } | ||
4949 | } | ||
4950 | queueIndex = -1; | ||
4951 | len = queue.length; | ||
4952 | } | ||
4953 | currentQueue = null; | ||
4954 | draining = false; | ||
4955 | runClearTimeout(timeout); | ||
4956 | } | ||
4957 | |||
4958 | process.nextTick = function (fun) { | ||
4959 | var args = new Array(arguments.length - 1); | ||
4960 | if (arguments.length > 1) { | ||
4961 | for (var i = 1; i < arguments.length; i++) { | ||
4962 | args[i - 1] = arguments[i]; | ||
4963 | } | ||
4964 | } | ||
4965 | queue.push(new Item(fun, args)); | ||
4966 | if (queue.length === 1 && !draining) { | ||
4967 | runTimeout(drainQueue); | ||
4968 | } | ||
4969 | }; | ||
4970 | |||
4971 | // v8 likes predictible objects | ||
4972 | function Item(fun, array) { | ||
4973 | this.fun = fun; | ||
4974 | this.array = array; | ||
4975 | } | ||
4976 | Item.prototype.run = function () { | ||
4977 | this.fun.apply(null, this.array); | ||
4978 | }; | ||
4979 | process.title = 'browser'; | ||
4980 | process.browser = true; | ||
4981 | process.env = {}; | ||
4982 | process.argv = []; | ||
4983 | process.version = ''; // empty string to avoid regexp issues | ||
4984 | process.versions = {}; | ||
4985 | |||
4986 | function noop() {} | ||
4987 | |||
4988 | process.on = noop; | ||
4989 | process.addListener = noop; | ||
4990 | process.once = noop; | ||
4991 | process.off = noop; | ||
4992 | process.removeListener = noop; | ||
4993 | process.removeAllListeners = noop; | ||
4994 | process.emit = noop; | ||
4995 | process.prependListener = noop; | ||
4996 | process.prependOnceListener = noop; | ||
4997 | |||
4998 | process.listeners = function (name) { return [] } | ||
4999 | |||
5000 | process.binding = function (name) { | ||
5001 | throw new Error('process.binding is not supported'); | ||
5002 | }; | ||
5003 | |||
5004 | process.cwd = function () { return '/' }; | ||
5005 | process.chdir = function (dir) { | ||
5006 | throw new Error('process.chdir is not supported'); | ||
5007 | }; | ||
5008 | process.umask = function() { return 0; }; | ||
5009 | |||
5010 | },{}],26:[function(require,module,exports){ | ||
5011 | module.exports = require('./lib/_stream_duplex.js'); | ||
5012 | |||
5013 | },{"./lib/_stream_duplex.js":27}],27:[function(require,module,exports){ | ||
5014 | // Copyright Joyent, Inc. and other Node contributors. | ||
5015 | // | ||
5016 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5017 | // copy of this software and associated documentation files (the | ||
5018 | // "Software"), to deal in the Software without restriction, including | ||
5019 | // without limitation the rights to use, copy, modify, merge, publish, | ||
5020 | // distribute, sublicense, and/or sell copies of the Software, and to permit | ||
5021 | // persons to whom the Software is furnished to do so, subject to the | ||
5022 | // following conditions: | ||
5023 | // | ||
5024 | // The above copyright notice and this permission notice shall be included | ||
5025 | // in all copies or substantial portions of the Software. | ||
5026 | // | ||
5027 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
5028 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
5029 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
5030 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
5031 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
5032 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
5033 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
5034 | |||
5035 | // a duplex stream is just a stream that is both readable and writable. | ||
5036 | // Since JS doesn't have multiple prototypal inheritance, this class | ||
5037 | // prototypally inherits from Readable, and then parasitically from | ||
5038 | // Writable. | ||
5039 | |||
5040 | 'use strict'; | ||
5041 | |||
5042 | /*<replacement>*/ | ||
5043 | |||
5044 | var processNextTick = require('process-nextick-args'); | ||
5045 | /*</replacement>*/ | ||
5046 | |||
5047 | /*<replacement>*/ | ||
5048 | var objectKeys = Object.keys || function (obj) { | ||
5049 | var keys = []; | ||
5050 | for (var key in obj) { | ||
5051 | keys.push(key); | ||
5052 | }return keys; | ||
5053 | }; | ||
5054 | /*</replacement>*/ | ||
5055 | |||
5056 | module.exports = Duplex; | ||
5057 | |||
5058 | /*<replacement>*/ | ||
5059 | var util = require('core-util-is'); | ||
5060 | util.inherits = require('inherits'); | ||
5061 | /*</replacement>*/ | ||
5062 | |||
5063 | var Readable = require('./_stream_readable'); | ||
5064 | var Writable = require('./_stream_writable'); | ||
5065 | |||
5066 | util.inherits(Duplex, Readable); | ||
5067 | |||
5068 | var keys = objectKeys(Writable.prototype); | ||
5069 | for (var v = 0; v < keys.length; v++) { | ||
5070 | var method = keys[v]; | ||
5071 | if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method]; | ||
5072 | } | ||
5073 | |||
5074 | function Duplex(options) { | ||
5075 | if (!(this instanceof Duplex)) return new Duplex(options); | ||
5076 | |||
5077 | Readable.call(this, options); | ||
5078 | Writable.call(this, options); | ||
5079 | |||
5080 | if (options && options.readable === false) this.readable = false; | ||
5081 | |||
5082 | if (options && options.writable === false) this.writable = false; | ||
5083 | |||
5084 | this.allowHalfOpen = true; | ||
5085 | if (options && options.allowHalfOpen === false) this.allowHalfOpen = false; | ||
5086 | |||
5087 | this.once('end', onend); | ||
5088 | } | ||
5089 | |||
5090 | // the no-half-open enforcer | ||
5091 | function onend() { | ||
5092 | // if we allow half-open state, or if the writable side ended, | ||
5093 | // then we're ok. | ||
5094 | if (this.allowHalfOpen || this._writableState.ended) return; | ||
5095 | |||
5096 | // no more data can be written. | ||
5097 | // But allow more writes to happen in this tick. | ||
5098 | processNextTick(onEndNT, this); | ||
5099 | } | ||
5100 | |||
5101 | function onEndNT(self) { | ||
5102 | self.end(); | ||
5103 | } | ||
5104 | |||
5105 | Object.defineProperty(Duplex.prototype, 'destroyed', { | ||
5106 | get: function () { | ||
5107 | if (this._readableState === undefined || this._writableState === undefined) { | ||
5108 | return false; | ||
5109 | } | ||
5110 | return this._readableState.destroyed && this._writableState.destroyed; | ||
5111 | }, | ||
5112 | set: function (value) { | ||
5113 | // we ignore the value if the stream | ||
5114 | // has not been initialized yet | ||
5115 | if (this._readableState === undefined || this._writableState === undefined) { | ||
5116 | return; | ||
5117 | } | ||
5118 | |||
5119 | // backward compatibility, the user is explicitly | ||
5120 | // managing destroyed | ||
5121 | this._readableState.destroyed = value; | ||
5122 | this._writableState.destroyed = value; | ||
5123 | } | ||
5124 | }); | ||
5125 | |||
5126 | Duplex.prototype._destroy = function (err, cb) { | ||
5127 | this.push(null); | ||
5128 | this.end(); | ||
5129 | |||
5130 | processNextTick(cb, err); | ||
5131 | }; | ||
5132 | |||
5133 | function forEach(xs, f) { | ||
5134 | for (var i = 0, l = xs.length; i < l; i++) { | ||
5135 | f(xs[i], i); | ||
5136 | } | ||
5137 | } | ||
5138 | },{"./_stream_readable":29,"./_stream_writable":31,"core-util-is":14,"inherits":21,"process-nextick-args":24}],28:[function(require,module,exports){ | ||
5139 | // Copyright Joyent, Inc. and other Node contributors. | ||
5140 | // | ||
5141 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5142 | // copy of this software and associated documentation files (the | ||
5143 | // "Software"), to deal in the Software without restriction, including | ||
5144 | // without limitation the rights to use, copy, modify, merge, publish, | ||
5145 | // distribute, sublicense, and/or sell copies of the Software, and to permit | ||
5146 | // persons to whom the Software is furnished to do so, subject to the | ||
5147 | // following conditions: | ||
5148 | // | ||
5149 | // The above copyright notice and this permission notice shall be included | ||
5150 | // in all copies or substantial portions of the Software. | ||
5151 | // | ||
5152 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
5153 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
5154 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
5155 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
5156 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
5157 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
5158 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
5159 | |||
5160 | // a passthrough stream. | ||
5161 | // basically just the most minimal sort of Transform stream. | ||
5162 | // Every written chunk gets output as-is. | ||
5163 | |||
5164 | 'use strict'; | ||
5165 | |||
5166 | module.exports = PassThrough; | ||
5167 | |||
5168 | var Transform = require('./_stream_transform'); | ||
5169 | |||
5170 | /*<replacement>*/ | ||
5171 | var util = require('core-util-is'); | ||
5172 | util.inherits = require('inherits'); | ||
5173 | /*</replacement>*/ | ||
5174 | |||
5175 | util.inherits(PassThrough, Transform); | ||
5176 | |||
5177 | function PassThrough(options) { | ||
5178 | if (!(this instanceof PassThrough)) return new PassThrough(options); | ||
5179 | |||
5180 | Transform.call(this, options); | ||
5181 | } | ||
5182 | |||
5183 | PassThrough.prototype._transform = function (chunk, encoding, cb) { | ||
5184 | cb(null, chunk); | ||
5185 | }; | ||
5186 | },{"./_stream_transform":30,"core-util-is":14,"inherits":21}],29:[function(require,module,exports){ | ||
5187 | (function (process,global){ | ||
5188 | // Copyright Joyent, Inc. and other Node contributors. | ||
5189 | // | ||
5190 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5191 | // copy of this software and associated documentation files (the | ||
5192 | // "Software"), to deal in the Software without restriction, including | ||
5193 | // without limitation the rights to use, copy, modify, merge, publish, | ||
5194 | // distribute, sublicense, and/or sell copies of the Software, and to permit | ||
5195 | // persons to whom the Software is furnished to do so, subject to the | ||
5196 | // following conditions: | ||
5197 | // | ||
5198 | // The above copyright notice and this permission notice shall be included | ||
5199 | // in all copies or substantial portions of the Software. | ||
5200 | // | ||
5201 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
5202 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
5203 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
5204 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
5205 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
5206 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
5207 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
5208 | |||
5209 | 'use strict'; | ||
5210 | |||
5211 | /*<replacement>*/ | ||
5212 | |||
5213 | var processNextTick = require('process-nextick-args'); | ||
5214 | /*</replacement>*/ | ||
5215 | |||
5216 | module.exports = Readable; | ||
5217 | |||
5218 | /*<replacement>*/ | ||
5219 | var isArray = require('isarray'); | ||
5220 | /*</replacement>*/ | ||
5221 | |||
5222 | /*<replacement>*/ | ||
5223 | var Duplex; | ||
5224 | /*</replacement>*/ | ||
5225 | |||
5226 | Readable.ReadableState = ReadableState; | ||
5227 | |||
5228 | /*<replacement>*/ | ||
5229 | var EE = require('events').EventEmitter; | ||
5230 | |||
5231 | var EElistenerCount = function (emitter, type) { | ||
5232 | return emitter.listeners(type).length; | ||
5233 | }; | ||
5234 | /*</replacement>*/ | ||
5235 | |||
5236 | /*<replacement>*/ | ||
5237 | var Stream = require('./internal/streams/stream'); | ||
5238 | /*</replacement>*/ | ||
5239 | |||
5240 | // TODO(bmeurer): Change this back to const once hole checks are | ||
5241 | // properly optimized away early in Ignition+TurboFan. | ||
5242 | /*<replacement>*/ | ||
5243 | var Buffer = require('safe-buffer').Buffer; | ||
5244 | var OurUint8Array = global.Uint8Array || function () {}; | ||
5245 | function _uint8ArrayToBuffer(chunk) { | ||
5246 | return Buffer.from(chunk); | ||
5247 | } | ||
5248 | function _isUint8Array(obj) { | ||
5249 | return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; | ||
5250 | } | ||
5251 | /*</replacement>*/ | ||
5252 | |||
5253 | /*<replacement>*/ | ||
5254 | var util = require('core-util-is'); | ||
5255 | util.inherits = require('inherits'); | ||
5256 | /*</replacement>*/ | ||
5257 | |||
5258 | /*<replacement>*/ | ||
5259 | var debugUtil = require('util'); | ||
5260 | var debug = void 0; | ||
5261 | if (debugUtil && debugUtil.debuglog) { | ||
5262 | debug = debugUtil.debuglog('stream'); | ||
5263 | } else { | ||
5264 | debug = function () {}; | ||
5265 | } | ||
5266 | /*</replacement>*/ | ||
5267 | |||
5268 | var BufferList = require('./internal/streams/BufferList'); | ||
5269 | var destroyImpl = require('./internal/streams/destroy'); | ||
5270 | var StringDecoder; | ||
5271 | |||
5272 | util.inherits(Readable, Stream); | ||
5273 | |||
5274 | var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume']; | ||
5275 | |||
5276 | function prependListener(emitter, event, fn) { | ||
5277 | // Sadly this is not cacheable as some libraries bundle their own | ||
5278 | // event emitter implementation with them. | ||
5279 | if (typeof emitter.prependListener === 'function') { | ||
5280 | return emitter.prependListener(event, fn); | ||
5281 | } else { | ||
5282 | // This is a hack to make sure that our error handler is attached before any | ||
5283 | // userland ones. NEVER DO THIS. This is here only because this code needs | ||
5284 | // to continue to work with older versions of Node.js that do not include | ||
5285 | // the prependListener() method. The goal is to eventually remove this hack. | ||
5286 | if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]]; | ||
5287 | } | ||
5288 | } | ||
5289 | |||
5290 | function ReadableState(options, stream) { | ||
5291 | Duplex = Duplex || require('./_stream_duplex'); | ||
5292 | |||
5293 | options = options || {}; | ||
5294 | |||
5295 | // object stream flag. Used to make read(n) ignore n and to | ||
5296 | // make all the buffer merging and length checks go away | ||
5297 | this.objectMode = !!options.objectMode; | ||
5298 | |||
5299 | if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode; | ||
5300 | |||
5301 | // the point at which it stops calling _read() to fill the buffer | ||
5302 | // Note: 0 is a valid value, means "don't call _read preemptively ever" | ||
5303 | var hwm = options.highWaterMark; | ||
5304 | var defaultHwm = this.objectMode ? 16 : 16 * 1024; | ||
5305 | this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm; | ||
5306 | |||
5307 | // cast to ints. | ||
5308 | this.highWaterMark = Math.floor(this.highWaterMark); | ||
5309 | |||
5310 | // A linked list is used to store data chunks instead of an array because the | ||
5311 | // linked list can remove elements from the beginning faster than | ||
5312 | // array.shift() | ||
5313 | this.buffer = new BufferList(); | ||
5314 | this.length = 0; | ||
5315 | this.pipes = null; | ||
5316 | this.pipesCount = 0; | ||
5317 | this.flowing = null; | ||
5318 | this.ended = false; | ||
5319 | this.endEmitted = false; | ||
5320 | this.reading = false; | ||
5321 | |||
5322 | // a flag to be able to tell if the event 'readable'/'data' is emitted | ||
5323 | // immediately, or on a later tick. We set this to true at first, because | ||
5324 | // any actions that shouldn't happen until "later" should generally also | ||
5325 | // not happen before the first read call. | ||
5326 | this.sync = true; | ||
5327 | |||
5328 | // whenever we return null, then we set a flag to say | ||
5329 | // that we're awaiting a 'readable' event emission. | ||
5330 | this.needReadable = false; | ||
5331 | this.emittedReadable = false; | ||
5332 | this.readableListening = false; | ||
5333 | this.resumeScheduled = false; | ||
5334 | |||
5335 | // has it been destroyed | ||
5336 | this.destroyed = false; | ||
5337 | |||
5338 | // Crypto is kind of old and crusty. Historically, its default string | ||
5339 | // encoding is 'binary' so we have to make this configurable. | ||
5340 | // Everything else in the universe uses 'utf8', though. | ||
5341 | this.defaultEncoding = options.defaultEncoding || 'utf8'; | ||
5342 | |||
5343 | // the number of writers that are awaiting a drain event in .pipe()s | ||
5344 | this.awaitDrain = 0; | ||
5345 | |||
5346 | // if true, a maybeReadMore has been scheduled | ||
5347 | this.readingMore = false; | ||
5348 | |||
5349 | this.decoder = null; | ||
5350 | this.encoding = null; | ||
5351 | if (options.encoding) { | ||
5352 | if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; | ||
5353 | this.decoder = new StringDecoder(options.encoding); | ||
5354 | this.encoding = options.encoding; | ||
5355 | } | ||
5356 | } | ||
5357 | |||
5358 | function Readable(options) { | ||
5359 | Duplex = Duplex || require('./_stream_duplex'); | ||
5360 | |||
5361 | if (!(this instanceof Readable)) return new Readable(options); | ||
5362 | |||
5363 | this._readableState = new ReadableState(options, this); | ||
5364 | |||
5365 | // legacy | ||
5366 | this.readable = true; | ||
5367 | |||
5368 | if (options) { | ||
5369 | if (typeof options.read === 'function') this._read = options.read; | ||
5370 | |||
5371 | if (typeof options.destroy === 'function') this._destroy = options.destroy; | ||
5372 | } | ||
5373 | |||
5374 | Stream.call(this); | ||
5375 | } | ||
5376 | |||
5377 | Object.defineProperty(Readable.prototype, 'destroyed', { | ||
5378 | get: function () { | ||
5379 | if (this._readableState === undefined) { | ||
5380 | return false; | ||
5381 | } | ||
5382 | return this._readableState.destroyed; | ||
5383 | }, | ||
5384 | set: function (value) { | ||
5385 | // we ignore the value if the stream | ||
5386 | // has not been initialized yet | ||
5387 | if (!this._readableState) { | ||
5388 | return; | ||
5389 | } | ||
5390 | |||
5391 | // backward compatibility, the user is explicitly | ||
5392 | // managing destroyed | ||
5393 | this._readableState.destroyed = value; | ||
5394 | } | ||
5395 | }); | ||
5396 | |||
5397 | Readable.prototype.destroy = destroyImpl.destroy; | ||
5398 | Readable.prototype._undestroy = destroyImpl.undestroy; | ||
5399 | Readable.prototype._destroy = function (err, cb) { | ||
5400 | this.push(null); | ||
5401 | cb(err); | ||
5402 | }; | ||
5403 | |||
5404 | // Manually shove something into the read() buffer. | ||
5405 | // This returns true if the highWaterMark has not been hit yet, | ||
5406 | // similar to how Writable.write() returns true if you should | ||
5407 | // write() some more. | ||
5408 | Readable.prototype.push = function (chunk, encoding) { | ||
5409 | var state = this._readableState; | ||
5410 | var skipChunkCheck; | ||
5411 | |||
5412 | if (!state.objectMode) { | ||
5413 | if (typeof chunk === 'string') { | ||
5414 | encoding = encoding || state.defaultEncoding; | ||
5415 | if (encoding !== state.encoding) { | ||
5416 | chunk = Buffer.from(chunk, encoding); | ||
5417 | encoding = ''; | ||
5418 | } | ||
5419 | skipChunkCheck = true; | ||
5420 | } | ||
5421 | } else { | ||
5422 | skipChunkCheck = true; | ||
5423 | } | ||
5424 | |||
5425 | return readableAddChunk(this, chunk, encoding, false, skipChunkCheck); | ||
5426 | }; | ||
5427 | |||
5428 | // Unshift should *always* be something directly out of read() | ||
5429 | Readable.prototype.unshift = function (chunk) { | ||
5430 | return readableAddChunk(this, chunk, null, true, false); | ||
5431 | }; | ||
5432 | |||
5433 | function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) { | ||
5434 | var state = stream._readableState; | ||
5435 | if (chunk === null) { | ||
5436 | state.reading = false; | ||
5437 | onEofChunk(stream, state); | ||
5438 | } else { | ||
5439 | var er; | ||
5440 | if (!skipChunkCheck) er = chunkInvalid(state, chunk); | ||
5441 | if (er) { | ||
5442 | stream.emit('error', er); | ||
5443 | } else if (state.objectMode || chunk && chunk.length > 0) { | ||
5444 | if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer.prototype) { | ||
5445 | chunk = _uint8ArrayToBuffer(chunk); | ||
5446 | } | ||
5447 | |||
5448 | if (addToFront) { | ||
5449 | if (state.endEmitted) stream.emit('error', new Error('stream.unshift() after end event'));else addChunk(stream, state, chunk, true); | ||
5450 | } else if (state.ended) { | ||
5451 | stream.emit('error', new Error('stream.push() after EOF')); | ||
5452 | } else { | ||
5453 | state.reading = false; | ||
5454 | if (state.decoder && !encoding) { | ||
5455 | chunk = state.decoder.write(chunk); | ||
5456 | if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false);else maybeReadMore(stream, state); | ||
5457 | } else { | ||
5458 | addChunk(stream, state, chunk, false); | ||
5459 | } | ||
5460 | } | ||
5461 | } else if (!addToFront) { | ||
5462 | state.reading = false; | ||
5463 | } | ||
5464 | } | ||
5465 | |||
5466 | return needMoreData(state); | ||
5467 | } | ||
5468 | |||
5469 | function addChunk(stream, state, chunk, addToFront) { | ||
5470 | if (state.flowing && state.length === 0 && !state.sync) { | ||
5471 | stream.emit('data', chunk); | ||
5472 | stream.read(0); | ||
5473 | } else { | ||
5474 | // update the buffer info. | ||
5475 | state.length += state.objectMode ? 1 : chunk.length; | ||
5476 | if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk); | ||
5477 | |||
5478 | if (state.needReadable) emitReadable(stream); | ||
5479 | } | ||
5480 | maybeReadMore(stream, state); | ||
5481 | } | ||
5482 | |||
5483 | function chunkInvalid(state, chunk) { | ||
5484 | var er; | ||
5485 | if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { | ||
5486 | er = new TypeError('Invalid non-string/buffer chunk'); | ||
5487 | } | ||
5488 | return er; | ||
5489 | } | ||
5490 | |||
5491 | // if it's past the high water mark, we can push in some more. | ||
5492 | // Also, if we have no data yet, we can stand some | ||
5493 | // more bytes. This is to work around cases where hwm=0, | ||
5494 | // such as the repl. Also, if the push() triggered a | ||
5495 | // readable event, and the user called read(largeNumber) such that | ||
5496 | // needReadable was set, then we ought to push more, so that another | ||
5497 | // 'readable' event will be triggered. | ||
5498 | function needMoreData(state) { | ||
5499 | return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0); | ||
5500 | } | ||
5501 | |||
5502 | Readable.prototype.isPaused = function () { | ||
5503 | return this._readableState.flowing === false; | ||
5504 | }; | ||
5505 | |||
5506 | // backwards compatibility. | ||
5507 | Readable.prototype.setEncoding = function (enc) { | ||
5508 | if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; | ||
5509 | this._readableState.decoder = new StringDecoder(enc); | ||
5510 | this._readableState.encoding = enc; | ||
5511 | return this; | ||
5512 | }; | ||
5513 | |||
5514 | // Don't raise the hwm > 8MB | ||
5515 | var MAX_HWM = 0x800000; | ||
5516 | function computeNewHighWaterMark(n) { | ||
5517 | if (n >= MAX_HWM) { | ||
5518 | n = MAX_HWM; | ||
5519 | } else { | ||
5520 | // Get the next highest power of 2 to prevent increasing hwm excessively in | ||
5521 | // tiny amounts | ||
5522 | n--; | ||
5523 | n |= n >>> 1; | ||
5524 | n |= n >>> 2; | ||
5525 | n |= n >>> 4; | ||
5526 | n |= n >>> 8; | ||
5527 | n |= n >>> 16; | ||
5528 | n++; | ||
5529 | } | ||
5530 | return n; | ||
5531 | } | ||
5532 | |||
5533 | // This function is designed to be inlinable, so please take care when making | ||
5534 | // changes to the function body. | ||
5535 | function howMuchToRead(n, state) { | ||
5536 | if (n <= 0 || state.length === 0 && state.ended) return 0; | ||
5537 | if (state.objectMode) return 1; | ||
5538 | if (n !== n) { | ||
5539 | // Only flow one buffer at a time | ||
5540 | if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length; | ||
5541 | } | ||
5542 | // If we're asking for more than the current hwm, then raise the hwm. | ||
5543 | if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n); | ||
5544 | if (n <= state.length) return n; | ||
5545 | // Don't have enough | ||
5546 | if (!state.ended) { | ||
5547 | state.needReadable = true; | ||
5548 | return 0; | ||
5549 | } | ||
5550 | return state.length; | ||
5551 | } | ||
5552 | |||
5553 | // you can override either this method, or the async _read(n) below. | ||
5554 | Readable.prototype.read = function (n) { | ||
5555 | debug('read', n); | ||
5556 | n = parseInt(n, 10); | ||
5557 | var state = this._readableState; | ||
5558 | var nOrig = n; | ||
5559 | |||
5560 | if (n !== 0) state.emittedReadable = false; | ||
5561 | |||
5562 | // if we're doing read(0) to trigger a readable event, but we | ||
5563 | // already have a bunch of data in the buffer, then just trigger | ||
5564 | // the 'readable' event and move on. | ||
5565 | if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) { | ||
5566 | debug('read: emitReadable', state.length, state.ended); | ||
5567 | if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this); | ||
5568 | return null; | ||
5569 | } | ||
5570 | |||
5571 | n = howMuchToRead(n, state); | ||
5572 | |||
5573 | // if we've ended, and we're now clear, then finish it up. | ||
5574 | if (n === 0 && state.ended) { | ||
5575 | if (state.length === 0) endReadable(this); | ||
5576 | return null; | ||
5577 | } | ||
5578 | |||
5579 | // All the actual chunk generation logic needs to be | ||
5580 | // *below* the call to _read. The reason is that in certain | ||
5581 | // synthetic stream cases, such as passthrough streams, _read | ||
5582 | // may be a completely synchronous operation which may change | ||
5583 | // the state of the read buffer, providing enough data when | ||
5584 | // before there was *not* enough. | ||
5585 | // | ||
5586 | // So, the steps are: | ||
5587 | // 1. Figure out what the state of things will be after we do | ||
5588 | // a read from the buffer. | ||
5589 | // | ||
5590 | // 2. If that resulting state will trigger a _read, then call _read. | ||
5591 | // Note that this may be asynchronous, or synchronous. Yes, it is | ||
5592 | // deeply ugly to write APIs this way, but that still doesn't mean | ||
5593 | // that the Readable class should behave improperly, as streams are | ||
5594 | // designed to be sync/async agnostic. | ||
5595 | // Take note if the _read call is sync or async (ie, if the read call | ||
5596 | // has returned yet), so that we know whether or not it's safe to emit | ||
5597 | // 'readable' etc. | ||
5598 | // | ||
5599 | // 3. Actually pull the requested chunks out of the buffer and return. | ||
5600 | |||
5601 | // if we need a readable event, then we need to do some reading. | ||
5602 | var doRead = state.needReadable; | ||
5603 | debug('need readable', doRead); | ||
5604 | |||
5605 | // if we currently have less than the highWaterMark, then also read some | ||
5606 | if (state.length === 0 || state.length - n < state.highWaterMark) { | ||
5607 | doRead = true; | ||
5608 | debug('length less than watermark', doRead); | ||
5609 | } | ||
5610 | |||
5611 | // however, if we've ended, then there's no point, and if we're already | ||
5612 | // reading, then it's unnecessary. | ||
5613 | if (state.ended || state.reading) { | ||
5614 | doRead = false; | ||
5615 | debug('reading or ended', doRead); | ||
5616 | } else if (doRead) { | ||
5617 | debug('do read'); | ||
5618 | state.reading = true; | ||
5619 | state.sync = true; | ||
5620 | // if the length is currently zero, then we *need* a readable event. | ||
5621 | if (state.length === 0) state.needReadable = true; | ||
5622 | // call internal read method | ||
5623 | this._read(state.highWaterMark); | ||
5624 | state.sync = false; | ||
5625 | // If _read pushed data synchronously, then `reading` will be false, | ||
5626 | // and we need to re-evaluate how much data we can return to the user. | ||
5627 | if (!state.reading) n = howMuchToRead(nOrig, state); | ||
5628 | } | ||
5629 | |||
5630 | var ret; | ||
5631 | if (n > 0) ret = fromList(n, state);else ret = null; | ||
5632 | |||
5633 | if (ret === null) { | ||
5634 | state.needReadable = true; | ||
5635 | n = 0; | ||
5636 | } else { | ||
5637 | state.length -= n; | ||
5638 | } | ||
5639 | |||
5640 | if (state.length === 0) { | ||
5641 | // If we have nothing in the buffer, then we want to know | ||
5642 | // as soon as we *do* get something into the buffer. | ||
5643 | if (!state.ended) state.needReadable = true; | ||
5644 | |||
5645 | // If we tried to read() past the EOF, then emit end on the next tick. | ||
5646 | if (nOrig !== n && state.ended) endReadable(this); | ||
5647 | } | ||
5648 | |||
5649 | if (ret !== null) this.emit('data', ret); | ||
5650 | |||
5651 | return ret; | ||
5652 | }; | ||
5653 | |||
5654 | function onEofChunk(stream, state) { | ||
5655 | if (state.ended) return; | ||
5656 | if (state.decoder) { | ||
5657 | var chunk = state.decoder.end(); | ||
5658 | if (chunk && chunk.length) { | ||
5659 | state.buffer.push(chunk); | ||
5660 | state.length += state.objectMode ? 1 : chunk.length; | ||
5661 | } | ||
5662 | } | ||
5663 | state.ended = true; | ||
5664 | |||
5665 | // emit 'readable' now to make sure it gets picked up. | ||
5666 | emitReadable(stream); | ||
5667 | } | ||
5668 | |||
5669 | // Don't emit readable right away in sync mode, because this can trigger | ||
5670 | // another read() call => stack overflow. This way, it might trigger | ||
5671 | // a nextTick recursion warning, but that's not so bad. | ||
5672 | function emitReadable(stream) { | ||
5673 | var state = stream._readableState; | ||
5674 | state.needReadable = false; | ||
5675 | if (!state.emittedReadable) { | ||
5676 | debug('emitReadable', state.flowing); | ||
5677 | state.emittedReadable = true; | ||
5678 | if (state.sync) processNextTick(emitReadable_, stream);else emitReadable_(stream); | ||
5679 | } | ||
5680 | } | ||
5681 | |||
5682 | function emitReadable_(stream) { | ||
5683 | debug('emit readable'); | ||
5684 | stream.emit('readable'); | ||
5685 | flow(stream); | ||
5686 | } | ||
5687 | |||
5688 | // at this point, the user has presumably seen the 'readable' event, | ||
5689 | // and called read() to consume some data. that may have triggered | ||
5690 | // in turn another _read(n) call, in which case reading = true if | ||
5691 | // it's in progress. | ||
5692 | // However, if we're not ended, or reading, and the length < hwm, | ||
5693 | // then go ahead and try to read some more preemptively. | ||
5694 | function maybeReadMore(stream, state) { | ||
5695 | if (!state.readingMore) { | ||
5696 | state.readingMore = true; | ||
5697 | processNextTick(maybeReadMore_, stream, state); | ||
5698 | } | ||
5699 | } | ||
5700 | |||
5701 | function maybeReadMore_(stream, state) { | ||
5702 | var len = state.length; | ||
5703 | while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) { | ||
5704 | debug('maybeReadMore read 0'); | ||
5705 | stream.read(0); | ||
5706 | if (len === state.length) | ||
5707 | // didn't get any data, stop spinning. | ||
5708 | break;else len = state.length; | ||
5709 | } | ||
5710 | state.readingMore = false; | ||
5711 | } | ||
5712 | |||
5713 | // abstract method. to be overridden in specific implementation classes. | ||
5714 | // call cb(er, data) where data is <= n in length. | ||
5715 | // for virtual (non-string, non-buffer) streams, "length" is somewhat | ||
5716 | // arbitrary, and perhaps not very meaningful. | ||
5717 | Readable.prototype._read = function (n) { | ||
5718 | this.emit('error', new Error('_read() is not implemented')); | ||
5719 | }; | ||
5720 | |||
5721 | Readable.prototype.pipe = function (dest, pipeOpts) { | ||
5722 | var src = this; | ||
5723 | var state = this._readableState; | ||
5724 | |||
5725 | switch (state.pipesCount) { | ||
5726 | case 0: | ||
5727 | state.pipes = dest; | ||
5728 | break; | ||
5729 | case 1: | ||
5730 | state.pipes = [state.pipes, dest]; | ||
5731 | break; | ||
5732 | default: | ||
5733 | state.pipes.push(dest); | ||
5734 | break; | ||
5735 | } | ||
5736 | state.pipesCount += 1; | ||
5737 | debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); | ||
5738 | |||
5739 | var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr; | ||
5740 | |||
5741 | var endFn = doEnd ? onend : unpipe; | ||
5742 | if (state.endEmitted) processNextTick(endFn);else src.once('end', endFn); | ||
5743 | |||
5744 | dest.on('unpipe', onunpipe); | ||
5745 | function onunpipe(readable, unpipeInfo) { | ||
5746 | debug('onunpipe'); | ||
5747 | if (readable === src) { | ||
5748 | if (unpipeInfo && unpipeInfo.hasUnpiped === false) { | ||
5749 | unpipeInfo.hasUnpiped = true; | ||
5750 | cleanup(); | ||
5751 | } | ||
5752 | } | ||
5753 | } | ||
5754 | |||
5755 | function onend() { | ||
5756 | debug('onend'); | ||
5757 | dest.end(); | ||
5758 | } | ||
5759 | |||
5760 | // when the dest drains, it reduces the awaitDrain counter | ||
5761 | // on the source. This would be more elegant with a .once() | ||
5762 | // handler in flow(), but adding and removing repeatedly is | ||
5763 | // too slow. | ||
5764 | var ondrain = pipeOnDrain(src); | ||
5765 | dest.on('drain', ondrain); | ||
5766 | |||
5767 | var cleanedUp = false; | ||
5768 | function cleanup() { | ||
5769 | debug('cleanup'); | ||
5770 | // cleanup event handlers once the pipe is broken | ||
5771 | dest.removeListener('close', onclose); | ||
5772 | dest.removeListener('finish', onfinish); | ||
5773 | dest.removeListener('drain', ondrain); | ||
5774 | dest.removeListener('error', onerror); | ||
5775 | dest.removeListener('unpipe', onunpipe); | ||
5776 | src.removeListener('end', onend); | ||
5777 | src.removeListener('end', unpipe); | ||
5778 | src.removeListener('data', ondata); | ||
5779 | |||
5780 | cleanedUp = true; | ||
5781 | |||
5782 | // if the reader is waiting for a drain event from this | ||
5783 | // specific writer, then it would cause it to never start | ||
5784 | // flowing again. | ||
5785 | // So, if this is awaiting a drain, then we just call it now. | ||
5786 | // If we don't know, then assume that we are waiting for one. | ||
5787 | if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain(); | ||
5788 | } | ||
5789 | |||
5790 | // If the user pushes more data while we're writing to dest then we'll end up | ||
5791 | // in ondata again. However, we only want to increase awaitDrain once because | ||
5792 | // dest will only emit one 'drain' event for the multiple writes. | ||
5793 | // => Introduce a guard on increasing awaitDrain. | ||
5794 | var increasedAwaitDrain = false; | ||
5795 | src.on('data', ondata); | ||
5796 | function ondata(chunk) { | ||
5797 | debug('ondata'); | ||
5798 | increasedAwaitDrain = false; | ||
5799 | var ret = dest.write(chunk); | ||
5800 | if (false === ret && !increasedAwaitDrain) { | ||
5801 | // If the user unpiped during `dest.write()`, it is possible | ||
5802 | // to get stuck in a permanently paused state if that write | ||
5803 | // also returned false. | ||
5804 | // => Check whether `dest` is still a piping destination. | ||
5805 | if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) { | ||
5806 | debug('false write response, pause', src._readableState.awaitDrain); | ||
5807 | src._readableState.awaitDrain++; | ||
5808 | increasedAwaitDrain = true; | ||
5809 | } | ||
5810 | src.pause(); | ||
5811 | } | ||
5812 | } | ||
5813 | |||
5814 | // if the dest has an error, then stop piping into it. | ||
5815 | // however, don't suppress the throwing behavior for this. | ||
5816 | function onerror(er) { | ||
5817 | debug('onerror', er); | ||
5818 | unpipe(); | ||
5819 | dest.removeListener('error', onerror); | ||
5820 | if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er); | ||
5821 | } | ||
5822 | |||
5823 | // Make sure our error handler is attached before userland ones. | ||
5824 | prependListener(dest, 'error', onerror); | ||
5825 | |||
5826 | // Both close and finish should trigger unpipe, but only once. | ||
5827 | function onclose() { | ||
5828 | dest.removeListener('finish', onfinish); | ||
5829 | unpipe(); | ||
5830 | } | ||
5831 | dest.once('close', onclose); | ||
5832 | function onfinish() { | ||
5833 | debug('onfinish'); | ||
5834 | dest.removeListener('close', onclose); | ||
5835 | unpipe(); | ||
5836 | } | ||
5837 | dest.once('finish', onfinish); | ||
5838 | |||
5839 | function unpipe() { | ||
5840 | debug('unpipe'); | ||
5841 | src.unpipe(dest); | ||
5842 | } | ||
5843 | |||
5844 | // tell the dest that it's being piped to | ||
5845 | dest.emit('pipe', src); | ||
5846 | |||
5847 | // start the flow if it hasn't been started already. | ||
5848 | if (!state.flowing) { | ||
5849 | debug('pipe resume'); | ||
5850 | src.resume(); | ||
5851 | } | ||
5852 | |||
5853 | return dest; | ||
5854 | }; | ||
5855 | |||
5856 | function pipeOnDrain(src) { | ||
5857 | return function () { | ||
5858 | var state = src._readableState; | ||
5859 | debug('pipeOnDrain', state.awaitDrain); | ||
5860 | if (state.awaitDrain) state.awaitDrain--; | ||
5861 | if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) { | ||
5862 | state.flowing = true; | ||
5863 | flow(src); | ||
5864 | } | ||
5865 | }; | ||
5866 | } | ||
5867 | |||
5868 | Readable.prototype.unpipe = function (dest) { | ||
5869 | var state = this._readableState; | ||
5870 | var unpipeInfo = { hasUnpiped: false }; | ||
5871 | |||
5872 | // if we're not piping anywhere, then do nothing. | ||
5873 | if (state.pipesCount === 0) return this; | ||
5874 | |||
5875 | // just one destination. most common case. | ||
5876 | if (state.pipesCount === 1) { | ||
5877 | // passed in one, but it's not the right one. | ||
5878 | if (dest && dest !== state.pipes) return this; | ||
5879 | |||
5880 | if (!dest) dest = state.pipes; | ||
5881 | |||
5882 | // got a match. | ||
5883 | state.pipes = null; | ||
5884 | state.pipesCount = 0; | ||
5885 | state.flowing = false; | ||
5886 | if (dest) dest.emit('unpipe', this, unpipeInfo); | ||
5887 | return this; | ||
5888 | } | ||
5889 | |||
5890 | // slow case. multiple pipe destinations. | ||
5891 | |||
5892 | if (!dest) { | ||
5893 | // remove all. | ||
5894 | var dests = state.pipes; | ||
5895 | var len = state.pipesCount; | ||
5896 | state.pipes = null; | ||
5897 | state.pipesCount = 0; | ||
5898 | state.flowing = false; | ||
5899 | |||
5900 | for (var i = 0; i < len; i++) { | ||
5901 | dests[i].emit('unpipe', this, unpipeInfo); | ||
5902 | }return this; | ||
5903 | } | ||
5904 | |||
5905 | // try to find the right one. | ||
5906 | var index = indexOf(state.pipes, dest); | ||
5907 | if (index === -1) return this; | ||
5908 | |||
5909 | state.pipes.splice(index, 1); | ||
5910 | state.pipesCount -= 1; | ||
5911 | if (state.pipesCount === 1) state.pipes = state.pipes[0]; | ||
5912 | |||
5913 | dest.emit('unpipe', this, unpipeInfo); | ||
5914 | |||
5915 | return this; | ||
5916 | }; | ||
5917 | |||
5918 | // set up data events if they are asked for | ||
5919 | // Ensure readable listeners eventually get something | ||
5920 | Readable.prototype.on = function (ev, fn) { | ||
5921 | var res = Stream.prototype.on.call(this, ev, fn); | ||
5922 | |||
5923 | if (ev === 'data') { | ||
5924 | // Start flowing on next tick if stream isn't explicitly paused | ||
5925 | if (this._readableState.flowing !== false) this.resume(); | ||
5926 | } else if (ev === 'readable') { | ||
5927 | var state = this._readableState; | ||
5928 | if (!state.endEmitted && !state.readableListening) { | ||
5929 | state.readableListening = state.needReadable = true; | ||
5930 | state.emittedReadable = false; | ||
5931 | if (!state.reading) { | ||
5932 | processNextTick(nReadingNextTick, this); | ||
5933 | } else if (state.length) { | ||
5934 | emitReadable(this); | ||
5935 | } | ||
5936 | } | ||
5937 | } | ||
5938 | |||
5939 | return res; | ||
5940 | }; | ||
5941 | Readable.prototype.addListener = Readable.prototype.on; | ||
5942 | |||
5943 | function nReadingNextTick(self) { | ||
5944 | debug('readable nexttick read 0'); | ||
5945 | self.read(0); | ||
5946 | } | ||
5947 | |||
5948 | // pause() and resume() are remnants of the legacy readable stream API | ||
5949 | // If the user uses them, then switch into old mode. | ||
5950 | Readable.prototype.resume = function () { | ||
5951 | var state = this._readableState; | ||
5952 | if (!state.flowing) { | ||
5953 | debug('resume'); | ||
5954 | state.flowing = true; | ||
5955 | resume(this, state); | ||
5956 | } | ||
5957 | return this; | ||
5958 | }; | ||
5959 | |||
5960 | function resume(stream, state) { | ||
5961 | if (!state.resumeScheduled) { | ||
5962 | state.resumeScheduled = true; | ||
5963 | processNextTick(resume_, stream, state); | ||
5964 | } | ||
5965 | } | ||
5966 | |||
5967 | function resume_(stream, state) { | ||
5968 | if (!state.reading) { | ||
5969 | debug('resume read 0'); | ||
5970 | stream.read(0); | ||
5971 | } | ||
5972 | |||
5973 | state.resumeScheduled = false; | ||
5974 | state.awaitDrain = 0; | ||
5975 | stream.emit('resume'); | ||
5976 | flow(stream); | ||
5977 | if (state.flowing && !state.reading) stream.read(0); | ||
5978 | } | ||
5979 | |||
5980 | Readable.prototype.pause = function () { | ||
5981 | debug('call pause flowing=%j', this._readableState.flowing); | ||
5982 | if (false !== this._readableState.flowing) { | ||
5983 | debug('pause'); | ||
5984 | this._readableState.flowing = false; | ||
5985 | this.emit('pause'); | ||
5986 | } | ||
5987 | return this; | ||
5988 | }; | ||
5989 | |||
5990 | function flow(stream) { | ||
5991 | var state = stream._readableState; | ||
5992 | debug('flow', state.flowing); | ||
5993 | while (state.flowing && stream.read() !== null) {} | ||
5994 | } | ||
5995 | |||
5996 | // wrap an old-style stream as the async data source. | ||
5997 | // This is *not* part of the readable stream interface. | ||
5998 | // It is an ugly unfortunate mess of history. | ||
5999 | Readable.prototype.wrap = function (stream) { | ||
6000 | var state = this._readableState; | ||
6001 | var paused = false; | ||
6002 | |||
6003 | var self = this; | ||
6004 | stream.on('end', function () { | ||
6005 | debug('wrapped end'); | ||
6006 | if (state.decoder && !state.ended) { | ||
6007 | var chunk = state.decoder.end(); | ||
6008 | if (chunk && chunk.length) self.push(chunk); | ||
6009 | } | ||
6010 | |||
6011 | self.push(null); | ||
6012 | }); | ||
6013 | |||
6014 | stream.on('data', function (chunk) { | ||
6015 | debug('wrapped data'); | ||
6016 | if (state.decoder) chunk = state.decoder.write(chunk); | ||
6017 | |||
6018 | // don't skip over falsy values in objectMode | ||
6019 | if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return; | ||
6020 | |||
6021 | var ret = self.push(chunk); | ||
6022 | if (!ret) { | ||
6023 | paused = true; | ||
6024 | stream.pause(); | ||
6025 | } | ||
6026 | }); | ||
6027 | |||
6028 | // proxy all the other methods. | ||
6029 | // important when wrapping filters and duplexes. | ||
6030 | for (var i in stream) { | ||
6031 | if (this[i] === undefined && typeof stream[i] === 'function') { | ||
6032 | this[i] = function (method) { | ||
6033 | return function () { | ||
6034 | return stream[method].apply(stream, arguments); | ||
6035 | }; | ||
6036 | }(i); | ||
6037 | } | ||
6038 | } | ||
6039 | |||
6040 | // proxy certain important events. | ||
6041 | for (var n = 0; n < kProxyEvents.length; n++) { | ||
6042 | stream.on(kProxyEvents[n], self.emit.bind(self, kProxyEvents[n])); | ||
6043 | } | ||
6044 | |||
6045 | // when we try to consume some more bytes, simply unpause the | ||
6046 | // underlying stream. | ||
6047 | self._read = function (n) { | ||
6048 | debug('wrapped _read', n); | ||
6049 | if (paused) { | ||
6050 | paused = false; | ||
6051 | stream.resume(); | ||
6052 | } | ||
6053 | }; | ||
6054 | |||
6055 | return self; | ||
6056 | }; | ||
6057 | |||
6058 | // exposed for testing purposes only. | ||
6059 | Readable._fromList = fromList; | ||
6060 | |||
6061 | // Pluck off n bytes from an array of buffers. | ||
6062 | // Length is the combined lengths of all the buffers in the list. | ||
6063 | // This function is designed to be inlinable, so please take care when making | ||
6064 | // changes to the function body. | ||
6065 | function fromList(n, state) { | ||
6066 | // nothing buffered | ||
6067 | if (state.length === 0) return null; | ||
6068 | |||
6069 | var ret; | ||
6070 | if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) { | ||
6071 | // read it all, truncate the list | ||
6072 | if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.head.data;else ret = state.buffer.concat(state.length); | ||
6073 | state.buffer.clear(); | ||
6074 | } else { | ||
6075 | // read part of list | ||
6076 | ret = fromListPartial(n, state.buffer, state.decoder); | ||
6077 | } | ||
6078 | |||
6079 | return ret; | ||
6080 | } | ||
6081 | |||
6082 | // Extracts only enough buffered data to satisfy the amount requested. | ||
6083 | // This function is designed to be inlinable, so please take care when making | ||
6084 | // changes to the function body. | ||
6085 | function fromListPartial(n, list, hasStrings) { | ||
6086 | var ret; | ||
6087 | if (n < list.head.data.length) { | ||
6088 | // slice is the same for buffers and strings | ||
6089 | ret = list.head.data.slice(0, n); | ||
6090 | list.head.data = list.head.data.slice(n); | ||
6091 | } else if (n === list.head.data.length) { | ||
6092 | // first chunk is a perfect match | ||
6093 | ret = list.shift(); | ||
6094 | } else { | ||
6095 | // result spans more than one buffer | ||
6096 | ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list); | ||
6097 | } | ||
6098 | return ret; | ||
6099 | } | ||
6100 | |||
6101 | // Copies a specified amount of characters from the list of buffered data | ||
6102 | // chunks. | ||
6103 | // This function is designed to be inlinable, so please take care when making | ||
6104 | // changes to the function body. | ||
6105 | function copyFromBufferString(n, list) { | ||
6106 | var p = list.head; | ||
6107 | var c = 1; | ||
6108 | var ret = p.data; | ||
6109 | n -= ret.length; | ||
6110 | while (p = p.next) { | ||
6111 | var str = p.data; | ||
6112 | var nb = n > str.length ? str.length : n; | ||
6113 | if (nb === str.length) ret += str;else ret += str.slice(0, n); | ||
6114 | n -= nb; | ||
6115 | if (n === 0) { | ||
6116 | if (nb === str.length) { | ||
6117 | ++c; | ||
6118 | if (p.next) list.head = p.next;else list.head = list.tail = null; | ||
6119 | } else { | ||
6120 | list.head = p; | ||
6121 | p.data = str.slice(nb); | ||
6122 | } | ||
6123 | break; | ||
6124 | } | ||
6125 | ++c; | ||
6126 | } | ||
6127 | list.length -= c; | ||
6128 | return ret; | ||
6129 | } | ||
6130 | |||
6131 | // Copies a specified amount of bytes from the list of buffered data chunks. | ||
6132 | // This function is designed to be inlinable, so please take care when making | ||
6133 | // changes to the function body. | ||
6134 | function copyFromBuffer(n, list) { | ||
6135 | var ret = Buffer.allocUnsafe(n); | ||
6136 | var p = list.head; | ||
6137 | var c = 1; | ||
6138 | p.data.copy(ret); | ||
6139 | n -= p.data.length; | ||
6140 | while (p = p.next) { | ||
6141 | var buf = p.data; | ||
6142 | var nb = n > buf.length ? buf.length : n; | ||
6143 | buf.copy(ret, ret.length - n, 0, nb); | ||
6144 | n -= nb; | ||
6145 | if (n === 0) { | ||
6146 | if (nb === buf.length) { | ||
6147 | ++c; | ||
6148 | if (p.next) list.head = p.next;else list.head = list.tail = null; | ||
6149 | } else { | ||
6150 | list.head = p; | ||
6151 | p.data = buf.slice(nb); | ||
6152 | } | ||
6153 | break; | ||
6154 | } | ||
6155 | ++c; | ||
6156 | } | ||
6157 | list.length -= c; | ||
6158 | return ret; | ||
6159 | } | ||
6160 | |||
6161 | function endReadable(stream) { | ||
6162 | var state = stream._readableState; | ||
6163 | |||
6164 | // If we get here before consuming all the bytes, then that is a | ||
6165 | // bug in node. Should never happen. | ||
6166 | if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream'); | ||
6167 | |||
6168 | if (!state.endEmitted) { | ||
6169 | state.ended = true; | ||
6170 | processNextTick(endReadableNT, state, stream); | ||
6171 | } | ||
6172 | } | ||
6173 | |||
6174 | function endReadableNT(state, stream) { | ||
6175 | // Check that we didn't get one last unshift. | ||
6176 | if (!state.endEmitted && state.length === 0) { | ||
6177 | state.endEmitted = true; | ||
6178 | stream.readable = false; | ||
6179 | stream.emit('end'); | ||
6180 | } | ||
6181 | } | ||
6182 | |||
6183 | function forEach(xs, f) { | ||
6184 | for (var i = 0, l = xs.length; i < l; i++) { | ||
6185 | f(xs[i], i); | ||
6186 | } | ||
6187 | } | ||
6188 | |||
6189 | function indexOf(xs, x) { | ||
6190 | for (var i = 0, l = xs.length; i < l; i++) { | ||
6191 | if (xs[i] === x) return i; | ||
6192 | } | ||
6193 | return -1; | ||
6194 | } | ||
6195 | }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | ||
6196 | },{"./_stream_duplex":27,"./internal/streams/BufferList":32,"./internal/streams/destroy":33,"./internal/streams/stream":34,"_process":25,"core-util-is":14,"events":18,"inherits":21,"isarray":23,"process-nextick-args":24,"safe-buffer":40,"string_decoder/":50,"util":4}],30:[function(require,module,exports){ | ||
6197 | // Copyright Joyent, Inc. and other Node contributors. | ||
6198 | // | ||
6199 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
6200 | // copy of this software and associated documentation files (the | ||
6201 | // "Software"), to deal in the Software without restriction, including | ||
6202 | // without limitation the rights to use, copy, modify, merge, publish, | ||
6203 | // distribute, sublicense, and/or sell copies of the Software, and to permit | ||
6204 | // persons to whom the Software is furnished to do so, subject to the | ||
6205 | // following conditions: | ||
6206 | // | ||
6207 | // The above copyright notice and this permission notice shall be included | ||
6208 | // in all copies or substantial portions of the Software. | ||
6209 | // | ||
6210 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
6211 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
6212 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
6213 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
6214 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
6215 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
6216 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
6217 | |||
6218 | // a transform stream is a readable/writable stream where you do | ||
6219 | // something with the data. Sometimes it's called a "filter", | ||
6220 | // but that's not a great name for it, since that implies a thing where | ||
6221 | // some bits pass through, and others are simply ignored. (That would | ||
6222 | // be a valid example of a transform, of course.) | ||
6223 | // | ||
6224 | // While the output is causally related to the input, it's not a | ||
6225 | // necessarily symmetric or synchronous transformation. For example, | ||
6226 | // a zlib stream might take multiple plain-text writes(), and then | ||
6227 | // emit a single compressed chunk some time in the future. | ||
6228 | // | ||
6229 | // Here's how this works: | ||
6230 | // | ||
6231 | // The Transform stream has all the aspects of the readable and writable | ||
6232 | // stream classes. When you write(chunk), that calls _write(chunk,cb) | ||
6233 | // internally, and returns false if there's a lot of pending writes | ||
6234 | // buffered up. When you call read(), that calls _read(n) until | ||
6235 | // there's enough pending readable data buffered up. | ||
6236 | // | ||
6237 | // In a transform stream, the written data is placed in a buffer. When | ||
6238 | // _read(n) is called, it transforms the queued up data, calling the | ||
6239 | // buffered _write cb's as it consumes chunks. If consuming a single | ||
6240 | // written chunk would result in multiple output chunks, then the first | ||
6241 | // outputted bit calls the readcb, and subsequent chunks just go into | ||
6242 | // the read buffer, and will cause it to emit 'readable' if necessary. | ||
6243 | // | ||
6244 | // This way, back-pressure is actually determined by the reading side, | ||
6245 | // since _read has to be called to start processing a new chunk. However, | ||
6246 | // a pathological inflate type of transform can cause excessive buffering | ||
6247 | // here. For example, imagine a stream where every byte of input is | ||
6248 | // interpreted as an integer from 0-255, and then results in that many | ||
6249 | // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in | ||
6250 | // 1kb of data being output. In this case, you could write a very small | ||
6251 | // amount of input, and end up with a very large amount of output. In | ||
6252 | // such a pathological inflating mechanism, there'd be no way to tell | ||
6253 | // the system to stop doing the transform. A single 4MB write could | ||
6254 | // cause the system to run out of memory. | ||
6255 | // | ||
6256 | // However, even in such a pathological case, only a single written chunk | ||
6257 | // would be consumed, and then the rest would wait (un-transformed) until | ||
6258 | // the results of the previous transformed chunk were consumed. | ||
6259 | |||
6260 | 'use strict'; | ||
6261 | |||
6262 | module.exports = Transform; | ||
6263 | |||
6264 | var Duplex = require('./_stream_duplex'); | ||
6265 | |||
6266 | /*<replacement>*/ | ||
6267 | var util = require('core-util-is'); | ||
6268 | util.inherits = require('inherits'); | ||
6269 | /*</replacement>*/ | ||
6270 | |||
6271 | util.inherits(Transform, Duplex); | ||
6272 | |||
6273 | function TransformState(stream) { | ||
6274 | this.afterTransform = function (er, data) { | ||
6275 | return afterTransform(stream, er, data); | ||
6276 | }; | ||
6277 | |||
6278 | this.needTransform = false; | ||
6279 | this.transforming = false; | ||
6280 | this.writecb = null; | ||
6281 | this.writechunk = null; | ||
6282 | this.writeencoding = null; | ||
6283 | } | ||
6284 | |||
6285 | function afterTransform(stream, er, data) { | ||
6286 | var ts = stream._transformState; | ||
6287 | ts.transforming = false; | ||
6288 | |||
6289 | var cb = ts.writecb; | ||
6290 | |||
6291 | if (!cb) { | ||
6292 | return stream.emit('error', new Error('write callback called multiple times')); | ||
6293 | } | ||
6294 | |||
6295 | ts.writechunk = null; | ||
6296 | ts.writecb = null; | ||
6297 | |||
6298 | if (data !== null && data !== undefined) stream.push(data); | ||
6299 | |||
6300 | cb(er); | ||
6301 | |||
6302 | var rs = stream._readableState; | ||
6303 | rs.reading = false; | ||
6304 | if (rs.needReadable || rs.length < rs.highWaterMark) { | ||
6305 | stream._read(rs.highWaterMark); | ||
6306 | } | ||
6307 | } | ||
6308 | |||
6309 | function Transform(options) { | ||
6310 | if (!(this instanceof Transform)) return new Transform(options); | ||
6311 | |||
6312 | Duplex.call(this, options); | ||
6313 | |||
6314 | this._transformState = new TransformState(this); | ||
6315 | |||
6316 | var stream = this; | ||
6317 | |||
6318 | // start out asking for a readable event once data is transformed. | ||
6319 | this._readableState.needReadable = true; | ||
6320 | |||
6321 | // we have implemented the _read method, and done the other things | ||
6322 | // that Readable wants before the first _read call, so unset the | ||
6323 | // sync guard flag. | ||
6324 | this._readableState.sync = false; | ||
6325 | |||
6326 | if (options) { | ||
6327 | if (typeof options.transform === 'function') this._transform = options.transform; | ||
6328 | |||
6329 | if (typeof options.flush === 'function') this._flush = options.flush; | ||
6330 | } | ||
6331 | |||
6332 | // When the writable side finishes, then flush out anything remaining. | ||
6333 | this.once('prefinish', function () { | ||
6334 | if (typeof this._flush === 'function') this._flush(function (er, data) { | ||
6335 | done(stream, er, data); | ||
6336 | });else done(stream); | ||
6337 | }); | ||
6338 | } | ||
6339 | |||
6340 | Transform.prototype.push = function (chunk, encoding) { | ||
6341 | this._transformState.needTransform = false; | ||
6342 | return Duplex.prototype.push.call(this, chunk, encoding); | ||
6343 | }; | ||
6344 | |||
6345 | // This is the part where you do stuff! | ||
6346 | // override this function in implementation classes. | ||
6347 | // 'chunk' is an input chunk. | ||
6348 | // | ||
6349 | // Call `push(newChunk)` to pass along transformed output | ||
6350 | // to the readable side. You may call 'push' zero or more times. | ||
6351 | // | ||
6352 | // Call `cb(err)` when you are done with this chunk. If you pass | ||
6353 | // an error, then that'll put the hurt on the whole operation. If you | ||
6354 | // never call cb(), then you'll never get another chunk. | ||
6355 | Transform.prototype._transform = function (chunk, encoding, cb) { | ||
6356 | throw new Error('_transform() is not implemented'); | ||
6357 | }; | ||
6358 | |||
6359 | Transform.prototype._write = function (chunk, encoding, cb) { | ||
6360 | var ts = this._transformState; | ||
6361 | ts.writecb = cb; | ||
6362 | ts.writechunk = chunk; | ||
6363 | ts.writeencoding = encoding; | ||
6364 | if (!ts.transforming) { | ||
6365 | var rs = this._readableState; | ||
6366 | if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark); | ||
6367 | } | ||
6368 | }; | ||
6369 | |||
6370 | // Doesn't matter what the args are here. | ||
6371 | // _transform does all the work. | ||
6372 | // That we got here means that the readable side wants more data. | ||
6373 | Transform.prototype._read = function (n) { | ||
6374 | var ts = this._transformState; | ||
6375 | |||
6376 | if (ts.writechunk !== null && ts.writecb && !ts.transforming) { | ||
6377 | ts.transforming = true; | ||
6378 | this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); | ||
6379 | } else { | ||
6380 | // mark that we need a transform, so that any data that comes in | ||
6381 | // will get processed, now that we've asked for it. | ||
6382 | ts.needTransform = true; | ||
6383 | } | ||
6384 | }; | ||
6385 | |||
6386 | Transform.prototype._destroy = function (err, cb) { | ||
6387 | var _this = this; | ||
6388 | |||
6389 | Duplex.prototype._destroy.call(this, err, function (err2) { | ||
6390 | cb(err2); | ||
6391 | _this.emit('close'); | ||
6392 | }); | ||
6393 | }; | ||
6394 | |||
6395 | function done(stream, er, data) { | ||
6396 | if (er) return stream.emit('error', er); | ||
6397 | |||
6398 | if (data !== null && data !== undefined) stream.push(data); | ||
6399 | |||
6400 | // if there's nothing in the write buffer, then that means | ||
6401 | // that nothing more will ever be provided | ||
6402 | var ws = stream._writableState; | ||
6403 | var ts = stream._transformState; | ||
6404 | |||
6405 | if (ws.length) throw new Error('Calling transform done when ws.length != 0'); | ||
6406 | |||
6407 | if (ts.transforming) throw new Error('Calling transform done when still transforming'); | ||
6408 | |||
6409 | return stream.push(null); | ||
6410 | } | ||
6411 | },{"./_stream_duplex":27,"core-util-is":14,"inherits":21}],31:[function(require,module,exports){ | ||
6412 | (function (process,global){ | ||
6413 | // Copyright Joyent, Inc. and other Node contributors. | ||
6414 | // | ||
6415 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
6416 | // copy of this software and associated documentation files (the | ||
6417 | // "Software"), to deal in the Software without restriction, including | ||
6418 | // without limitation the rights to use, copy, modify, merge, publish, | ||
6419 | // distribute, sublicense, and/or sell copies of the Software, and to permit | ||
6420 | // persons to whom the Software is furnished to do so, subject to the | ||
6421 | // following conditions: | ||
6422 | // | ||
6423 | // The above copyright notice and this permission notice shall be included | ||
6424 | // in all copies or substantial portions of the Software. | ||
6425 | // | ||
6426 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
6427 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
6428 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
6429 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
6430 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
6431 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
6432 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
6433 | |||
6434 | // A bit simpler than readable streams. | ||
6435 | // Implement an async ._write(chunk, encoding, cb), and it'll handle all | ||
6436 | // the drain event emission and buffering. | ||
6437 | |||
6438 | 'use strict'; | ||
6439 | |||
6440 | /*<replacement>*/ | ||
6441 | |||
6442 | var processNextTick = require('process-nextick-args'); | ||
6443 | /*</replacement>*/ | ||
6444 | |||
6445 | module.exports = Writable; | ||
6446 | |||
6447 | /* <replacement> */ | ||
6448 | function WriteReq(chunk, encoding, cb) { | ||
6449 | this.chunk = chunk; | ||
6450 | this.encoding = encoding; | ||
6451 | this.callback = cb; | ||
6452 | this.next = null; | ||
6453 | } | ||
6454 | |||
6455 | // It seems a linked list but it is not | ||
6456 | // there will be only 2 of these for each stream | ||
6457 | function CorkedRequest(state) { | ||
6458 | var _this = this; | ||
6459 | |||
6460 | this.next = null; | ||
6461 | this.entry = null; | ||
6462 | this.finish = function () { | ||
6463 | onCorkedFinish(_this, state); | ||
6464 | }; | ||
6465 | } | ||
6466 | /* </replacement> */ | ||
6467 | |||
6468 | /*<replacement>*/ | ||
6469 | var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : processNextTick; | ||
6470 | /*</replacement>*/ | ||
6471 | |||
6472 | /*<replacement>*/ | ||
6473 | var Duplex; | ||
6474 | /*</replacement>*/ | ||
6475 | |||
6476 | Writable.WritableState = WritableState; | ||
6477 | |||
6478 | /*<replacement>*/ | ||
6479 | var util = require('core-util-is'); | ||
6480 | util.inherits = require('inherits'); | ||
6481 | /*</replacement>*/ | ||
6482 | |||
6483 | /*<replacement>*/ | ||
6484 | var internalUtil = { | ||
6485 | deprecate: require('util-deprecate') | ||
6486 | }; | ||
6487 | /*</replacement>*/ | ||
6488 | |||
6489 | /*<replacement>*/ | ||
6490 | var Stream = require('./internal/streams/stream'); | ||
6491 | /*</replacement>*/ | ||
6492 | |||
6493 | /*<replacement>*/ | ||
6494 | var Buffer = require('safe-buffer').Buffer; | ||
6495 | var OurUint8Array = global.Uint8Array || function () {}; | ||
6496 | function _uint8ArrayToBuffer(chunk) { | ||
6497 | return Buffer.from(chunk); | ||
6498 | } | ||
6499 | function _isUint8Array(obj) { | ||
6500 | return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; | ||
6501 | } | ||
6502 | /*</replacement>*/ | ||
6503 | |||
6504 | var destroyImpl = require('./internal/streams/destroy'); | ||
6505 | |||
6506 | util.inherits(Writable, Stream); | ||
6507 | |||
6508 | function nop() {} | ||
6509 | |||
6510 | function WritableState(options, stream) { | ||
6511 | Duplex = Duplex || require('./_stream_duplex'); | ||
6512 | |||
6513 | options = options || {}; | ||
6514 | |||
6515 | // object stream flag to indicate whether or not this stream | ||
6516 | // contains buffers or objects. | ||
6517 | this.objectMode = !!options.objectMode; | ||
6518 | |||
6519 | if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.writableObjectMode; | ||
6520 | |||
6521 | // the point at which write() starts returning false | ||
6522 | // Note: 0 is a valid value, means that we always return false if | ||
6523 | // the entire buffer is not flushed immediately on write() | ||
6524 | var hwm = options.highWaterMark; | ||
6525 | var defaultHwm = this.objectMode ? 16 : 16 * 1024; | ||
6526 | this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm; | ||
6527 | |||
6528 | // cast to ints. | ||
6529 | this.highWaterMark = Math.floor(this.highWaterMark); | ||
6530 | |||
6531 | // if _final has been called | ||
6532 | this.finalCalled = false; | ||
6533 | |||
6534 | // drain event flag. | ||
6535 | this.needDrain = false; | ||
6536 | // at the start of calling end() | ||
6537 | this.ending = false; | ||
6538 | // when end() has been called, and returned | ||
6539 | this.ended = false; | ||
6540 | // when 'finish' is emitted | ||
6541 | this.finished = false; | ||
6542 | |||
6543 | // has it been destroyed | ||
6544 | this.destroyed = false; | ||
6545 | |||
6546 | // should we decode strings into buffers before passing to _write? | ||
6547 | // this is here so that some node-core streams can optimize string | ||
6548 | // handling at a lower level. | ||
6549 | var noDecode = options.decodeStrings === false; | ||
6550 | this.decodeStrings = !noDecode; | ||
6551 | |||
6552 | // Crypto is kind of old and crusty. Historically, its default string | ||
6553 | // encoding is 'binary' so we have to make this configurable. | ||
6554 | // Everything else in the universe uses 'utf8', though. | ||
6555 | this.defaultEncoding = options.defaultEncoding || 'utf8'; | ||
6556 | |||
6557 | // not an actual buffer we keep track of, but a measurement | ||
6558 | // of how much we're waiting to get pushed to some underlying | ||
6559 | // socket or file. | ||
6560 | this.length = 0; | ||
6561 | |||
6562 | // a flag to see when we're in the middle of a write. | ||
6563 | this.writing = false; | ||
6564 | |||
6565 | // when true all writes will be buffered until .uncork() call | ||
6566 | this.corked = 0; | ||
6567 | |||
6568 | // a flag to be able to tell if the onwrite cb is called immediately, | ||
6569 | // or on a later tick. We set this to true at first, because any | ||
6570 | // actions that shouldn't happen until "later" should generally also | ||
6571 | // not happen before the first write call. | ||
6572 | this.sync = true; | ||
6573 | |||
6574 | // a flag to know if we're processing previously buffered items, which | ||
6575 | // may call the _write() callback in the same tick, so that we don't | ||
6576 | // end up in an overlapped onwrite situation. | ||
6577 | this.bufferProcessing = false; | ||
6578 | |||
6579 | // the callback that's passed to _write(chunk,cb) | ||
6580 | this.onwrite = function (er) { | ||
6581 | onwrite(stream, er); | ||
6582 | }; | ||
6583 | |||
6584 | // the callback that the user supplies to write(chunk,encoding,cb) | ||
6585 | this.writecb = null; | ||
6586 | |||
6587 | // the amount that is being written when _write is called. | ||
6588 | this.writelen = 0; | ||
6589 | |||
6590 | this.bufferedRequest = null; | ||
6591 | this.lastBufferedRequest = null; | ||
6592 | |||
6593 | // number of pending user-supplied write callbacks | ||
6594 | // this must be 0 before 'finish' can be emitted | ||
6595 | this.pendingcb = 0; | ||
6596 | |||
6597 | // emit prefinish if the only thing we're waiting for is _write cbs | ||
6598 | // This is relevant for synchronous Transform streams | ||
6599 | this.prefinished = false; | ||
6600 | |||
6601 | // True if the error was already emitted and should not be thrown again | ||
6602 | this.errorEmitted = false; | ||
6603 | |||
6604 | // count buffered requests | ||
6605 | this.bufferedRequestCount = 0; | ||
6606 | |||
6607 | // allocate the first CorkedRequest, there is always | ||
6608 | // one allocated and free to use, and we maintain at most two | ||
6609 | this.corkedRequestsFree = new CorkedRequest(this); | ||
6610 | } | ||
6611 | |||
6612 | WritableState.prototype.getBuffer = function getBuffer() { | ||
6613 | var current = this.bufferedRequest; | ||
6614 | var out = []; | ||
6615 | while (current) { | ||
6616 | out.push(current); | ||
6617 | current = current.next; | ||
6618 | } | ||
6619 | return out; | ||
6620 | }; | ||
6621 | |||
6622 | (function () { | ||
6623 | try { | ||
6624 | Object.defineProperty(WritableState.prototype, 'buffer', { | ||
6625 | get: internalUtil.deprecate(function () { | ||
6626 | return this.getBuffer(); | ||
6627 | }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.', 'DEP0003') | ||
6628 | }); | ||
6629 | } catch (_) {} | ||
6630 | })(); | ||
6631 | |||
6632 | // Test _writableState for inheritance to account for Duplex streams, | ||
6633 | // whose prototype chain only points to Readable. | ||
6634 | var realHasInstance; | ||
6635 | if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') { | ||
6636 | realHasInstance = Function.prototype[Symbol.hasInstance]; | ||
6637 | Object.defineProperty(Writable, Symbol.hasInstance, { | ||
6638 | value: function (object) { | ||
6639 | if (realHasInstance.call(this, object)) return true; | ||
6640 | |||
6641 | return object && object._writableState instanceof WritableState; | ||
6642 | } | ||
6643 | }); | ||
6644 | } else { | ||
6645 | realHasInstance = function (object) { | ||
6646 | return object instanceof this; | ||
6647 | }; | ||
6648 | } | ||
6649 | |||
6650 | function Writable(options) { | ||
6651 | Duplex = Duplex || require('./_stream_duplex'); | ||
6652 | |||
6653 | // Writable ctor is applied to Duplexes, too. | ||
6654 | // `realHasInstance` is necessary because using plain `instanceof` | ||
6655 | // would return false, as no `_writableState` property is attached. | ||
6656 | |||
6657 | // Trying to use the custom `instanceof` for Writable here will also break the | ||
6658 | // Node.js LazyTransform implementation, which has a non-trivial getter for | ||
6659 | // `_writableState` that would lead to infinite recursion. | ||
6660 | if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) { | ||
6661 | return new Writable(options); | ||
6662 | } | ||
6663 | |||
6664 | this._writableState = new WritableState(options, this); | ||
6665 | |||
6666 | // legacy. | ||
6667 | this.writable = true; | ||
6668 | |||
6669 | if (options) { | ||
6670 | if (typeof options.write === 'function') this._write = options.write; | ||
6671 | |||
6672 | if (typeof options.writev === 'function') this._writev = options.writev; | ||
6673 | |||
6674 | if (typeof options.destroy === 'function') this._destroy = options.destroy; | ||
6675 | |||
6676 | if (typeof options.final === 'function') this._final = options.final; | ||
6677 | } | ||
6678 | |||
6679 | Stream.call(this); | ||
6680 | } | ||
6681 | |||
6682 | // Otherwise people can pipe Writable streams, which is just wrong. | ||
6683 | Writable.prototype.pipe = function () { | ||
6684 | this.emit('error', new Error('Cannot pipe, not readable')); | ||
6685 | }; | ||
6686 | |||
6687 | function writeAfterEnd(stream, cb) { | ||
6688 | var er = new Error('write after end'); | ||
6689 | // TODO: defer error events consistently everywhere, not just the cb | ||
6690 | stream.emit('error', er); | ||
6691 | processNextTick(cb, er); | ||
6692 | } | ||
6693 | |||
6694 | // Checks that a user-supplied chunk is valid, especially for the particular | ||
6695 | // mode the stream is in. Currently this means that `null` is never accepted | ||
6696 | // and undefined/non-string values are only allowed in object mode. | ||
6697 | function validChunk(stream, state, chunk, cb) { | ||
6698 | var valid = true; | ||
6699 | var er = false; | ||
6700 | |||
6701 | if (chunk === null) { | ||
6702 | er = new TypeError('May not write null values to stream'); | ||
6703 | } else if (typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { | ||
6704 | er = new TypeError('Invalid non-string/buffer chunk'); | ||
6705 | } | ||
6706 | if (er) { | ||
6707 | stream.emit('error', er); | ||
6708 | processNextTick(cb, er); | ||
6709 | valid = false; | ||
6710 | } | ||
6711 | return valid; | ||
6712 | } | ||
6713 | |||
6714 | Writable.prototype.write = function (chunk, encoding, cb) { | ||
6715 | var state = this._writableState; | ||
6716 | var ret = false; | ||
6717 | var isBuf = _isUint8Array(chunk) && !state.objectMode; | ||
6718 | |||
6719 | if (isBuf && !Buffer.isBuffer(chunk)) { | ||
6720 | chunk = _uint8ArrayToBuffer(chunk); | ||
6721 | } | ||
6722 | |||
6723 | if (typeof encoding === 'function') { | ||
6724 | cb = encoding; | ||
6725 | encoding = null; | ||
6726 | } | ||
6727 | |||
6728 | if (isBuf) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding; | ||
6729 | |||
6730 | if (typeof cb !== 'function') cb = nop; | ||
6731 | |||
6732 | if (state.ended) writeAfterEnd(this, cb);else if (isBuf || validChunk(this, state, chunk, cb)) { | ||
6733 | state.pendingcb++; | ||
6734 | ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb); | ||
6735 | } | ||
6736 | |||
6737 | return ret; | ||
6738 | }; | ||
6739 | |||
6740 | Writable.prototype.cork = function () { | ||
6741 | var state = this._writableState; | ||
6742 | |||
6743 | state.corked++; | ||
6744 | }; | ||
6745 | |||
6746 | Writable.prototype.uncork = function () { | ||
6747 | var state = this._writableState; | ||
6748 | |||
6749 | if (state.corked) { | ||
6750 | state.corked--; | ||
6751 | |||
6752 | if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state); | ||
6753 | } | ||
6754 | }; | ||
6755 | |||
6756 | Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) { | ||
6757 | // node::ParseEncoding() requires lower case. | ||
6758 | if (typeof encoding === 'string') encoding = encoding.toLowerCase(); | ||
6759 | if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new TypeError('Unknown encoding: ' + encoding); | ||
6760 | this._writableState.defaultEncoding = encoding; | ||
6761 | return this; | ||
6762 | }; | ||
6763 | |||
6764 | function decodeChunk(state, chunk, encoding) { | ||
6765 | if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') { | ||
6766 | chunk = Buffer.from(chunk, encoding); | ||
6767 | } | ||
6768 | return chunk; | ||
6769 | } | ||
6770 | |||
6771 | // if we're already writing something, then just put this | ||
6772 | // in the queue, and wait our turn. Otherwise, call _write | ||
6773 | // If we return false, then we need a drain event, so set that flag. | ||
6774 | function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) { | ||
6775 | if (!isBuf) { | ||
6776 | var newChunk = decodeChunk(state, chunk, encoding); | ||
6777 | if (chunk !== newChunk) { | ||
6778 | isBuf = true; | ||
6779 | encoding = 'buffer'; | ||
6780 | chunk = newChunk; | ||
6781 | } | ||
6782 | } | ||
6783 | var len = state.objectMode ? 1 : chunk.length; | ||
6784 | |||
6785 | state.length += len; | ||
6786 | |||
6787 | var ret = state.length < state.highWaterMark; | ||
6788 | // we must ensure that previous needDrain will not be reset to false. | ||
6789 | if (!ret) state.needDrain = true; | ||
6790 | |||
6791 | if (state.writing || state.corked) { | ||
6792 | var last = state.lastBufferedRequest; | ||
6793 | state.lastBufferedRequest = { | ||
6794 | chunk: chunk, | ||
6795 | encoding: encoding, | ||
6796 | isBuf: isBuf, | ||
6797 | callback: cb, | ||
6798 | next: null | ||
6799 | }; | ||
6800 | if (last) { | ||
6801 | last.next = state.lastBufferedRequest; | ||
6802 | } else { | ||
6803 | state.bufferedRequest = state.lastBufferedRequest; | ||
6804 | } | ||
6805 | state.bufferedRequestCount += 1; | ||
6806 | } else { | ||
6807 | doWrite(stream, state, false, len, chunk, encoding, cb); | ||
6808 | } | ||
6809 | |||
6810 | return ret; | ||
6811 | } | ||
6812 | |||
6813 | function doWrite(stream, state, writev, len, chunk, encoding, cb) { | ||
6814 | state.writelen = len; | ||
6815 | state.writecb = cb; | ||
6816 | state.writing = true; | ||
6817 | state.sync = true; | ||
6818 | if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite); | ||
6819 | state.sync = false; | ||
6820 | } | ||
6821 | |||
6822 | function onwriteError(stream, state, sync, er, cb) { | ||
6823 | --state.pendingcb; | ||
6824 | |||
6825 | if (sync) { | ||
6826 | // defer the callback if we are being called synchronously | ||
6827 | // to avoid piling up things on the stack | ||
6828 | processNextTick(cb, er); | ||
6829 | // this can emit finish, and it will always happen | ||
6830 | // after error | ||
6831 | processNextTick(finishMaybe, stream, state); | ||
6832 | stream._writableState.errorEmitted = true; | ||
6833 | stream.emit('error', er); | ||
6834 | } else { | ||
6835 | // the caller expect this to happen before if | ||
6836 | // it is async | ||
6837 | cb(er); | ||
6838 | stream._writableState.errorEmitted = true; | ||
6839 | stream.emit('error', er); | ||
6840 | // this can emit finish, but finish must | ||
6841 | // always follow error | ||
6842 | finishMaybe(stream, state); | ||
6843 | } | ||
6844 | } | ||
6845 | |||
6846 | function onwriteStateUpdate(state) { | ||
6847 | state.writing = false; | ||
6848 | state.writecb = null; | ||
6849 | state.length -= state.writelen; | ||
6850 | state.writelen = 0; | ||
6851 | } | ||
6852 | |||
6853 | function onwrite(stream, er) { | ||
6854 | var state = stream._writableState; | ||
6855 | var sync = state.sync; | ||
6856 | var cb = state.writecb; | ||
6857 | |||
6858 | onwriteStateUpdate(state); | ||
6859 | |||
6860 | if (er) onwriteError(stream, state, sync, er, cb);else { | ||
6861 | // Check if we're actually ready to finish, but don't emit yet | ||
6862 | var finished = needFinish(state); | ||
6863 | |||
6864 | if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) { | ||
6865 | clearBuffer(stream, state); | ||
6866 | } | ||
6867 | |||
6868 | if (sync) { | ||
6869 | /*<replacement>*/ | ||
6870 | asyncWrite(afterWrite, stream, state, finished, cb); | ||
6871 | /*</replacement>*/ | ||
6872 | } else { | ||
6873 | afterWrite(stream, state, finished, cb); | ||
6874 | } | ||
6875 | } | ||
6876 | } | ||
6877 | |||
6878 | function afterWrite(stream, state, finished, cb) { | ||
6879 | if (!finished) onwriteDrain(stream, state); | ||
6880 | state.pendingcb--; | ||
6881 | cb(); | ||
6882 | finishMaybe(stream, state); | ||
6883 | } | ||
6884 | |||
6885 | // Must force callback to be called on nextTick, so that we don't | ||
6886 | // emit 'drain' before the write() consumer gets the 'false' return | ||
6887 | // value, and has a chance to attach a 'drain' listener. | ||
6888 | function onwriteDrain(stream, state) { | ||
6889 | if (state.length === 0 && state.needDrain) { | ||
6890 | state.needDrain = false; | ||
6891 | stream.emit('drain'); | ||
6892 | } | ||
6893 | } | ||
6894 | |||
6895 | // if there's something in the buffer waiting, then process it | ||
6896 | function clearBuffer(stream, state) { | ||
6897 | state.bufferProcessing = true; | ||
6898 | var entry = state.bufferedRequest; | ||
6899 | |||
6900 | if (stream._writev && entry && entry.next) { | ||
6901 | // Fast case, write everything using _writev() | ||
6902 | var l = state.bufferedRequestCount; | ||
6903 | var buffer = new Array(l); | ||
6904 | var holder = state.corkedRequestsFree; | ||
6905 | holder.entry = entry; | ||
6906 | |||
6907 | var count = 0; | ||
6908 | var allBuffers = true; | ||
6909 | while (entry) { | ||
6910 | buffer[count] = entry; | ||
6911 | if (!entry.isBuf) allBuffers = false; | ||
6912 | entry = entry.next; | ||
6913 | count += 1; | ||
6914 | } | ||
6915 | buffer.allBuffers = allBuffers; | ||
6916 | |||
6917 | doWrite(stream, state, true, state.length, buffer, '', holder.finish); | ||
6918 | |||
6919 | // doWrite is almost always async, defer these to save a bit of time | ||
6920 | // as the hot path ends with doWrite | ||
6921 | state.pendingcb++; | ||
6922 | state.lastBufferedRequest = null; | ||
6923 | if (holder.next) { | ||
6924 | state.corkedRequestsFree = holder.next; | ||
6925 | holder.next = null; | ||
6926 | } else { | ||
6927 | state.corkedRequestsFree = new CorkedRequest(state); | ||
6928 | } | ||
6929 | } else { | ||
6930 | // Slow case, write chunks one-by-one | ||
6931 | while (entry) { | ||
6932 | var chunk = entry.chunk; | ||
6933 | var encoding = entry.encoding; | ||
6934 | var cb = entry.callback; | ||
6935 | var len = state.objectMode ? 1 : chunk.length; | ||
6936 | |||
6937 | doWrite(stream, state, false, len, chunk, encoding, cb); | ||
6938 | entry = entry.next; | ||
6939 | // if we didn't call the onwrite immediately, then | ||
6940 | // it means that we need to wait until it does. | ||
6941 | // also, that means that the chunk and cb are currently | ||
6942 | // being processed, so move the buffer counter past them. | ||
6943 | if (state.writing) { | ||
6944 | break; | ||
6945 | } | ||
6946 | } | ||
6947 | |||
6948 | if (entry === null) state.lastBufferedRequest = null; | ||
6949 | } | ||
6950 | |||
6951 | state.bufferedRequestCount = 0; | ||
6952 | state.bufferedRequest = entry; | ||
6953 | state.bufferProcessing = false; | ||
6954 | } | ||
6955 | |||
6956 | Writable.prototype._write = function (chunk, encoding, cb) { | ||
6957 | cb(new Error('_write() is not implemented')); | ||
6958 | }; | ||
6959 | |||
6960 | Writable.prototype._writev = null; | ||
6961 | |||
6962 | Writable.prototype.end = function (chunk, encoding, cb) { | ||
6963 | var state = this._writableState; | ||
6964 | |||
6965 | if (typeof chunk === 'function') { | ||
6966 | cb = chunk; | ||
6967 | chunk = null; | ||
6968 | encoding = null; | ||
6969 | } else if (typeof encoding === 'function') { | ||
6970 | cb = encoding; | ||
6971 | encoding = null; | ||
6972 | } | ||
6973 | |||
6974 | if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); | ||
6975 | |||
6976 | // .end() fully uncorks | ||
6977 | if (state.corked) { | ||
6978 | state.corked = 1; | ||
6979 | this.uncork(); | ||
6980 | } | ||
6981 | |||
6982 | // ignore unnecessary end() calls. | ||
6983 | if (!state.ending && !state.finished) endWritable(this, state, cb); | ||
6984 | }; | ||
6985 | |||
6986 | function needFinish(state) { | ||
6987 | return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing; | ||
6988 | } | ||
6989 | function callFinal(stream, state) { | ||
6990 | stream._final(function (err) { | ||
6991 | state.pendingcb--; | ||
6992 | if (err) { | ||
6993 | stream.emit('error', err); | ||
6994 | } | ||
6995 | state.prefinished = true; | ||
6996 | stream.emit('prefinish'); | ||
6997 | finishMaybe(stream, state); | ||
6998 | }); | ||
6999 | } | ||
7000 | function prefinish(stream, state) { | ||
7001 | if (!state.prefinished && !state.finalCalled) { | ||
7002 | if (typeof stream._final === 'function') { | ||
7003 | state.pendingcb++; | ||
7004 | state.finalCalled = true; | ||
7005 | processNextTick(callFinal, stream, state); | ||
7006 | } else { | ||
7007 | state.prefinished = true; | ||
7008 | stream.emit('prefinish'); | ||
7009 | } | ||
7010 | } | ||
7011 | } | ||
7012 | |||
7013 | function finishMaybe(stream, state) { | ||
7014 | var need = needFinish(state); | ||
7015 | if (need) { | ||
7016 | prefinish(stream, state); | ||
7017 | if (state.pendingcb === 0) { | ||
7018 | state.finished = true; | ||
7019 | stream.emit('finish'); | ||
7020 | } | ||
7021 | } | ||
7022 | return need; | ||
7023 | } | ||
7024 | |||
7025 | function endWritable(stream, state, cb) { | ||
7026 | state.ending = true; | ||
7027 | finishMaybe(stream, state); | ||
7028 | if (cb) { | ||
7029 | if (state.finished) processNextTick(cb);else stream.once('finish', cb); | ||
7030 | } | ||
7031 | state.ended = true; | ||
7032 | stream.writable = false; | ||
7033 | } | ||
7034 | |||
7035 | function onCorkedFinish(corkReq, state, err) { | ||
7036 | var entry = corkReq.entry; | ||
7037 | corkReq.entry = null; | ||
7038 | while (entry) { | ||
7039 | var cb = entry.callback; | ||
7040 | state.pendingcb--; | ||
7041 | cb(err); | ||
7042 | entry = entry.next; | ||
7043 | } | ||
7044 | if (state.corkedRequestsFree) { | ||
7045 | state.corkedRequestsFree.next = corkReq; | ||
7046 | } else { | ||
7047 | state.corkedRequestsFree = corkReq; | ||
7048 | } | ||
7049 | } | ||
7050 | |||
7051 | Object.defineProperty(Writable.prototype, 'destroyed', { | ||
7052 | get: function () { | ||
7053 | if (this._writableState === undefined) { | ||
7054 | return false; | ||
7055 | } | ||
7056 | return this._writableState.destroyed; | ||
7057 | }, | ||
7058 | set: function (value) { | ||
7059 | // we ignore the value if the stream | ||
7060 | // has not been initialized yet | ||
7061 | if (!this._writableState) { | ||
7062 | return; | ||
7063 | } | ||
7064 | |||
7065 | // backward compatibility, the user is explicitly | ||
7066 | // managing destroyed | ||
7067 | this._writableState.destroyed = value; | ||
7068 | } | ||
7069 | }); | ||
7070 | |||
7071 | Writable.prototype.destroy = destroyImpl.destroy; | ||
7072 | Writable.prototype._undestroy = destroyImpl.undestroy; | ||
7073 | Writable.prototype._destroy = function (err, cb) { | ||
7074 | this.end(); | ||
7075 | cb(err); | ||
7076 | }; | ||
7077 | }).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | ||
7078 | },{"./_stream_duplex":27,"./internal/streams/destroy":33,"./internal/streams/stream":34,"_process":25,"core-util-is":14,"inherits":21,"process-nextick-args":24,"safe-buffer":40,"util-deprecate":51}],32:[function(require,module,exports){ | ||
7079 | 'use strict'; | ||
7080 | |||
7081 | /*<replacement>*/ | ||
7082 | |||
7083 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
7084 | |||
7085 | var Buffer = require('safe-buffer').Buffer; | ||
7086 | /*</replacement>*/ | ||
7087 | |||
7088 | function copyBuffer(src, target, offset) { | ||
7089 | src.copy(target, offset); | ||
7090 | } | ||
7091 | |||
7092 | module.exports = function () { | ||
7093 | function BufferList() { | ||
7094 | _classCallCheck(this, BufferList); | ||
7095 | |||
7096 | this.head = null; | ||
7097 | this.tail = null; | ||
7098 | this.length = 0; | ||
7099 | } | ||
7100 | |||
7101 | BufferList.prototype.push = function push(v) { | ||
7102 | var entry = { data: v, next: null }; | ||
7103 | if (this.length > 0) this.tail.next = entry;else this.head = entry; | ||
7104 | this.tail = entry; | ||
7105 | ++this.length; | ||
7106 | }; | ||
7107 | |||
7108 | BufferList.prototype.unshift = function unshift(v) { | ||
7109 | var entry = { data: v, next: this.head }; | ||
7110 | if (this.length === 0) this.tail = entry; | ||
7111 | this.head = entry; | ||
7112 | ++this.length; | ||
7113 | }; | ||
7114 | |||
7115 | BufferList.prototype.shift = function shift() { | ||
7116 | if (this.length === 0) return; | ||
7117 | var ret = this.head.data; | ||
7118 | if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next; | ||
7119 | --this.length; | ||
7120 | return ret; | ||
7121 | }; | ||
7122 | |||
7123 | BufferList.prototype.clear = function clear() { | ||
7124 | this.head = this.tail = null; | ||
7125 | this.length = 0; | ||
7126 | }; | ||
7127 | |||
7128 | BufferList.prototype.join = function join(s) { | ||
7129 | if (this.length === 0) return ''; | ||
7130 | var p = this.head; | ||
7131 | var ret = '' + p.data; | ||
7132 | while (p = p.next) { | ||
7133 | ret += s + p.data; | ||
7134 | }return ret; | ||
7135 | }; | ||
7136 | |||
7137 | BufferList.prototype.concat = function concat(n) { | ||
7138 | if (this.length === 0) return Buffer.alloc(0); | ||
7139 | if (this.length === 1) return this.head.data; | ||
7140 | var ret = Buffer.allocUnsafe(n >>> 0); | ||
7141 | var p = this.head; | ||
7142 | var i = 0; | ||
7143 | while (p) { | ||
7144 | copyBuffer(p.data, ret, i); | ||
7145 | i += p.data.length; | ||
7146 | p = p.next; | ||
7147 | } | ||
7148 | return ret; | ||
7149 | }; | ||
7150 | |||
7151 | return BufferList; | ||
7152 | }(); | ||
7153 | },{"safe-buffer":40}],33:[function(require,module,exports){ | ||
7154 | 'use strict'; | ||
7155 | |||
7156 | /*<replacement>*/ | ||
7157 | |||
7158 | var processNextTick = require('process-nextick-args'); | ||
7159 | /*</replacement>*/ | ||
7160 | |||
7161 | // undocumented cb() API, needed for core, not for public API | ||
7162 | function destroy(err, cb) { | ||
7163 | var _this = this; | ||
7164 | |||
7165 | var readableDestroyed = this._readableState && this._readableState.destroyed; | ||
7166 | var writableDestroyed = this._writableState && this._writableState.destroyed; | ||
7167 | |||
7168 | if (readableDestroyed || writableDestroyed) { | ||
7169 | if (cb) { | ||
7170 | cb(err); | ||
7171 | } else if (err && (!this._writableState || !this._writableState.errorEmitted)) { | ||
7172 | processNextTick(emitErrorNT, this, err); | ||
7173 | } | ||
7174 | return; | ||
7175 | } | ||
7176 | |||
7177 | // we set destroyed to true before firing error callbacks in order | ||
7178 | // to make it re-entrance safe in case destroy() is called within callbacks | ||
7179 | |||
7180 | if (this._readableState) { | ||
7181 | this._readableState.destroyed = true; | ||
7182 | } | ||
7183 | |||
7184 | // if this is a duplex stream mark the writable part as destroyed as well | ||
7185 | if (this._writableState) { | ||
7186 | this._writableState.destroyed = true; | ||
7187 | } | ||
7188 | |||
7189 | this._destroy(err || null, function (err) { | ||
7190 | if (!cb && err) { | ||
7191 | processNextTick(emitErrorNT, _this, err); | ||
7192 | if (_this._writableState) { | ||
7193 | _this._writableState.errorEmitted = true; | ||
7194 | } | ||
7195 | } else if (cb) { | ||
7196 | cb(err); | ||
7197 | } | ||
7198 | }); | ||
7199 | } | ||
7200 | |||
7201 | function undestroy() { | ||
7202 | if (this._readableState) { | ||
7203 | this._readableState.destroyed = false; | ||
7204 | this._readableState.reading = false; | ||
7205 | this._readableState.ended = false; | ||
7206 | this._readableState.endEmitted = false; | ||
7207 | } | ||
7208 | |||
7209 | if (this._writableState) { | ||
7210 | this._writableState.destroyed = false; | ||
7211 | this._writableState.ended = false; | ||
7212 | this._writableState.ending = false; | ||
7213 | this._writableState.finished = false; | ||
7214 | this._writableState.errorEmitted = false; | ||
7215 | } | ||
7216 | } | ||
7217 | |||
7218 | function emitErrorNT(self, err) { | ||
7219 | self.emit('error', err); | ||
7220 | } | ||
7221 | |||
7222 | module.exports = { | ||
7223 | destroy: destroy, | ||
7224 | undestroy: undestroy | ||
7225 | }; | ||
7226 | },{"process-nextick-args":24}],34:[function(require,module,exports){ | ||
7227 | module.exports = require('events').EventEmitter; | ||
7228 | |||
7229 | },{"events":18}],35:[function(require,module,exports){ | ||
7230 | module.exports = require('./readable').PassThrough | ||
7231 | |||
7232 | },{"./readable":36}],36:[function(require,module,exports){ | ||
7233 | exports = module.exports = require('./lib/_stream_readable.js'); | ||
7234 | exports.Stream = exports; | ||
7235 | exports.Readable = exports; | ||
7236 | exports.Writable = require('./lib/_stream_writable.js'); | ||
7237 | exports.Duplex = require('./lib/_stream_duplex.js'); | ||
7238 | exports.Transform = require('./lib/_stream_transform.js'); | ||
7239 | exports.PassThrough = require('./lib/_stream_passthrough.js'); | ||
7240 | |||
7241 | },{"./lib/_stream_duplex.js":27,"./lib/_stream_passthrough.js":28,"./lib/_stream_readable.js":29,"./lib/_stream_transform.js":30,"./lib/_stream_writable.js":31}],37:[function(require,module,exports){ | ||
7242 | module.exports = require('./readable').Transform | ||
7243 | |||
7244 | },{"./readable":36}],38:[function(require,module,exports){ | ||
7245 | module.exports = require('./lib/_stream_writable.js'); | ||
7246 | |||
7247 | },{"./lib/_stream_writable.js":31}],39:[function(require,module,exports){ | ||
7248 | (function (Buffer){ | ||
7249 | 'use strict' | ||
7250 | var inherits = require('inherits') | ||
7251 | var HashBase = require('hash-base') | ||
7252 | |||
7253 | function RIPEMD160 () { | ||
7254 | HashBase.call(this, 64) | ||
7255 | |||
7256 | // state | ||
7257 | this._a = 0x67452301 | ||
7258 | this._b = 0xefcdab89 | ||
7259 | this._c = 0x98badcfe | ||
7260 | this._d = 0x10325476 | ||
7261 | this._e = 0xc3d2e1f0 | ||
7262 | } | ||
7263 | |||
7264 | inherits(RIPEMD160, HashBase) | ||
7265 | |||
7266 | RIPEMD160.prototype._update = function () { | ||
7267 | var m = new Array(16) | ||
7268 | for (var i = 0; i < 16; ++i) m[i] = this._block.readInt32LE(i * 4) | ||
7269 | |||
7270 | var al = this._a | ||
7271 | var bl = this._b | ||
7272 | var cl = this._c | ||
7273 | var dl = this._d | ||
7274 | var el = this._e | ||
7275 | |||
7276 | // Mj = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 | ||
7277 | // K = 0x00000000 | ||
7278 | // Sj = 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8 | ||
7279 | al = fn1(al, bl, cl, dl, el, m[0], 0x00000000, 11); cl = rotl(cl, 10) | ||
7280 | el = fn1(el, al, bl, cl, dl, m[1], 0x00000000, 14); bl = rotl(bl, 10) | ||
7281 | dl = fn1(dl, el, al, bl, cl, m[2], 0x00000000, 15); al = rotl(al, 10) | ||
7282 | cl = fn1(cl, dl, el, al, bl, m[3], 0x00000000, 12); el = rotl(el, 10) | ||
7283 | bl = fn1(bl, cl, dl, el, al, m[4], 0x00000000, 5); dl = rotl(dl, 10) | ||
7284 | al = fn1(al, bl, cl, dl, el, m[5], 0x00000000, 8); cl = rotl(cl, 10) | ||
7285 | el = fn1(el, al, bl, cl, dl, m[6], 0x00000000, 7); bl = rotl(bl, 10) | ||
7286 | dl = fn1(dl, el, al, bl, cl, m[7], 0x00000000, 9); al = rotl(al, 10) | ||
7287 | cl = fn1(cl, dl, el, al, bl, m[8], 0x00000000, 11); el = rotl(el, 10) | ||
7288 | bl = fn1(bl, cl, dl, el, al, m[9], 0x00000000, 13); dl = rotl(dl, 10) | ||
7289 | al = fn1(al, bl, cl, dl, el, m[10], 0x00000000, 14); cl = rotl(cl, 10) | ||
7290 | el = fn1(el, al, bl, cl, dl, m[11], 0x00000000, 15); bl = rotl(bl, 10) | ||
7291 | dl = fn1(dl, el, al, bl, cl, m[12], 0x00000000, 6); al = rotl(al, 10) | ||
7292 | cl = fn1(cl, dl, el, al, bl, m[13], 0x00000000, 7); el = rotl(el, 10) | ||
7293 | bl = fn1(bl, cl, dl, el, al, m[14], 0x00000000, 9); dl = rotl(dl, 10) | ||
7294 | al = fn1(al, bl, cl, dl, el, m[15], 0x00000000, 8); cl = rotl(cl, 10) | ||
7295 | |||
7296 | // Mj = 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8 | ||
7297 | // K = 0x5a827999 | ||
7298 | // Sj = 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12 | ||
7299 | el = fn2(el, al, bl, cl, dl, m[7], 0x5a827999, 7); bl = rotl(bl, 10) | ||
7300 | dl = fn2(dl, el, al, bl, cl, m[4], 0x5a827999, 6); al = rotl(al, 10) | ||
7301 | cl = fn2(cl, dl, el, al, bl, m[13], 0x5a827999, 8); el = rotl(el, 10) | ||
7302 | bl = fn2(bl, cl, dl, el, al, m[1], 0x5a827999, 13); dl = rotl(dl, 10) | ||
7303 | al = fn2(al, bl, cl, dl, el, m[10], 0x5a827999, 11); cl = rotl(cl, 10) | ||
7304 | el = fn2(el, al, bl, cl, dl, m[6], 0x5a827999, 9); bl = rotl(bl, 10) | ||
7305 | dl = fn2(dl, el, al, bl, cl, m[15], 0x5a827999, 7); al = rotl(al, 10) | ||
7306 | cl = fn2(cl, dl, el, al, bl, m[3], 0x5a827999, 15); el = rotl(el, 10) | ||
7307 | bl = fn2(bl, cl, dl, el, al, m[12], 0x5a827999, 7); dl = rotl(dl, 10) | ||
7308 | al = fn2(al, bl, cl, dl, el, m[0], 0x5a827999, 12); cl = rotl(cl, 10) | ||
7309 | el = fn2(el, al, bl, cl, dl, m[9], 0x5a827999, 15); bl = rotl(bl, 10) | ||
7310 | dl = fn2(dl, el, al, bl, cl, m[5], 0x5a827999, 9); al = rotl(al, 10) | ||
7311 | cl = fn2(cl, dl, el, al, bl, m[2], 0x5a827999, 11); el = rotl(el, 10) | ||
7312 | bl = fn2(bl, cl, dl, el, al, m[14], 0x5a827999, 7); dl = rotl(dl, 10) | ||
7313 | al = fn2(al, bl, cl, dl, el, m[11], 0x5a827999, 13); cl = rotl(cl, 10) | ||
7314 | el = fn2(el, al, bl, cl, dl, m[8], 0x5a827999, 12); bl = rotl(bl, 10) | ||
7315 | |||
7316 | // Mj = 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12 | ||
7317 | // K = 0x6ed9eba1 | ||
7318 | // Sj = 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5 | ||
7319 | dl = fn3(dl, el, al, bl, cl, m[3], 0x6ed9eba1, 11); al = rotl(al, 10) | ||
7320 | cl = fn3(cl, dl, el, al, bl, m[10], 0x6ed9eba1, 13); el = rotl(el, 10) | ||
7321 | bl = fn3(bl, cl, dl, el, al, m[14], 0x6ed9eba1, 6); dl = rotl(dl, 10) | ||
7322 | al = fn3(al, bl, cl, dl, el, m[4], 0x6ed9eba1, 7); cl = rotl(cl, 10) | ||
7323 | el = fn3(el, al, bl, cl, dl, m[9], 0x6ed9eba1, 14); bl = rotl(bl, 10) | ||
7324 | dl = fn3(dl, el, al, bl, cl, m[15], 0x6ed9eba1, 9); al = rotl(al, 10) | ||
7325 | cl = fn3(cl, dl, el, al, bl, m[8], 0x6ed9eba1, 13); el = rotl(el, 10) | ||
7326 | bl = fn3(bl, cl, dl, el, al, m[1], 0x6ed9eba1, 15); dl = rotl(dl, 10) | ||
7327 | al = fn3(al, bl, cl, dl, el, m[2], 0x6ed9eba1, 14); cl = rotl(cl, 10) | ||
7328 | el = fn3(el, al, bl, cl, dl, m[7], 0x6ed9eba1, 8); bl = rotl(bl, 10) | ||
7329 | dl = fn3(dl, el, al, bl, cl, m[0], 0x6ed9eba1, 13); al = rotl(al, 10) | ||
7330 | cl = fn3(cl, dl, el, al, bl, m[6], 0x6ed9eba1, 6); el = rotl(el, 10) | ||
7331 | bl = fn3(bl, cl, dl, el, al, m[13], 0x6ed9eba1, 5); dl = rotl(dl, 10) | ||
7332 | al = fn3(al, bl, cl, dl, el, m[11], 0x6ed9eba1, 12); cl = rotl(cl, 10) | ||
7333 | el = fn3(el, al, bl, cl, dl, m[5], 0x6ed9eba1, 7); bl = rotl(bl, 10) | ||
7334 | dl = fn3(dl, el, al, bl, cl, m[12], 0x6ed9eba1, 5); al = rotl(al, 10) | ||
7335 | |||
7336 | // Mj = 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2 | ||
7337 | // K = 0x8f1bbcdc | ||
7338 | // Sj = 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12 | ||
7339 | cl = fn4(cl, dl, el, al, bl, m[1], 0x8f1bbcdc, 11); el = rotl(el, 10) | ||
7340 | bl = fn4(bl, cl, dl, el, al, m[9], 0x8f1bbcdc, 12); dl = rotl(dl, 10) | ||
7341 | al = fn4(al, bl, cl, dl, el, m[11], 0x8f1bbcdc, 14); cl = rotl(cl, 10) | ||
7342 | el = fn4(el, al, bl, cl, dl, m[10], 0x8f1bbcdc, 15); bl = rotl(bl, 10) | ||
7343 | dl = fn4(dl, el, al, bl, cl, m[0], 0x8f1bbcdc, 14); al = rotl(al, 10) | ||
7344 | cl = fn4(cl, dl, el, al, bl, m[8], 0x8f1bbcdc, 15); el = rotl(el, 10) | ||
7345 | bl = fn4(bl, cl, dl, el, al, m[12], 0x8f1bbcdc, 9); dl = rotl(dl, 10) | ||
7346 | al = fn4(al, bl, cl, dl, el, m[4], 0x8f1bbcdc, 8); cl = rotl(cl, 10) | ||
7347 | el = fn4(el, al, bl, cl, dl, m[13], 0x8f1bbcdc, 9); bl = rotl(bl, 10) | ||
7348 | dl = fn4(dl, el, al, bl, cl, m[3], 0x8f1bbcdc, 14); al = rotl(al, 10) | ||
7349 | cl = fn4(cl, dl, el, al, bl, m[7], 0x8f1bbcdc, 5); el = rotl(el, 10) | ||
7350 | bl = fn4(bl, cl, dl, el, al, m[15], 0x8f1bbcdc, 6); dl = rotl(dl, 10) | ||
7351 | al = fn4(al, bl, cl, dl, el, m[14], 0x8f1bbcdc, 8); cl = rotl(cl, 10) | ||
7352 | el = fn4(el, al, bl, cl, dl, m[5], 0x8f1bbcdc, 6); bl = rotl(bl, 10) | ||
7353 | dl = fn4(dl, el, al, bl, cl, m[6], 0x8f1bbcdc, 5); al = rotl(al, 10) | ||
7354 | cl = fn4(cl, dl, el, al, bl, m[2], 0x8f1bbcdc, 12); el = rotl(el, 10) | ||
7355 | |||
7356 | // Mj = 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13 | ||
7357 | // K = 0xa953fd4e | ||
7358 | // Sj = 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 | ||
7359 | bl = fn5(bl, cl, dl, el, al, m[4], 0xa953fd4e, 9); dl = rotl(dl, 10) | ||
7360 | al = fn5(al, bl, cl, dl, el, m[0], 0xa953fd4e, 15); cl = rotl(cl, 10) | ||
7361 | el = fn5(el, al, bl, cl, dl, m[5], 0xa953fd4e, 5); bl = rotl(bl, 10) | ||
7362 | dl = fn5(dl, el, al, bl, cl, m[9], 0xa953fd4e, 11); al = rotl(al, 10) | ||
7363 | cl = fn5(cl, dl, el, al, bl, m[7], 0xa953fd4e, 6); el = rotl(el, 10) | ||
7364 | bl = fn5(bl, cl, dl, el, al, m[12], 0xa953fd4e, 8); dl = rotl(dl, 10) | ||
7365 | al = fn5(al, bl, cl, dl, el, m[2], 0xa953fd4e, 13); cl = rotl(cl, 10) | ||
7366 | el = fn5(el, al, bl, cl, dl, m[10], 0xa953fd4e, 12); bl = rotl(bl, 10) | ||
7367 | dl = fn5(dl, el, al, bl, cl, m[14], 0xa953fd4e, 5); al = rotl(al, 10) | ||
7368 | cl = fn5(cl, dl, el, al, bl, m[1], 0xa953fd4e, 12); el = rotl(el, 10) | ||
7369 | bl = fn5(bl, cl, dl, el, al, m[3], 0xa953fd4e, 13); dl = rotl(dl, 10) | ||
7370 | al = fn5(al, bl, cl, dl, el, m[8], 0xa953fd4e, 14); cl = rotl(cl, 10) | ||
7371 | el = fn5(el, al, bl, cl, dl, m[11], 0xa953fd4e, 11); bl = rotl(bl, 10) | ||
7372 | dl = fn5(dl, el, al, bl, cl, m[6], 0xa953fd4e, 8); al = rotl(al, 10) | ||
7373 | cl = fn5(cl, dl, el, al, bl, m[15], 0xa953fd4e, 5); el = rotl(el, 10) | ||
7374 | bl = fn5(bl, cl, dl, el, al, m[13], 0xa953fd4e, 6); dl = rotl(dl, 10) | ||
7375 | |||
7376 | var ar = this._a | ||
7377 | var br = this._b | ||
7378 | var cr = this._c | ||
7379 | var dr = this._d | ||
7380 | var er = this._e | ||
7381 | |||
7382 | // M'j = 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12 | ||
7383 | // K' = 0x50a28be6 | ||
7384 | // S'j = 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6 | ||
7385 | ar = fn5(ar, br, cr, dr, er, m[5], 0x50a28be6, 8); cr = rotl(cr, 10) | ||
7386 | er = fn5(er, ar, br, cr, dr, m[14], 0x50a28be6, 9); br = rotl(br, 10) | ||
7387 | dr = fn5(dr, er, ar, br, cr, m[7], 0x50a28be6, 9); ar = rotl(ar, 10) | ||
7388 | cr = fn5(cr, dr, er, ar, br, m[0], 0x50a28be6, 11); er = rotl(er, 10) | ||
7389 | br = fn5(br, cr, dr, er, ar, m[9], 0x50a28be6, 13); dr = rotl(dr, 10) | ||
7390 | ar = fn5(ar, br, cr, dr, er, m[2], 0x50a28be6, 15); cr = rotl(cr, 10) | ||
7391 | er = fn5(er, ar, br, cr, dr, m[11], 0x50a28be6, 15); br = rotl(br, 10) | ||
7392 | dr = fn5(dr, er, ar, br, cr, m[4], 0x50a28be6, 5); ar = rotl(ar, 10) | ||
7393 | cr = fn5(cr, dr, er, ar, br, m[13], 0x50a28be6, 7); er = rotl(er, 10) | ||
7394 | br = fn5(br, cr, dr, er, ar, m[6], 0x50a28be6, 7); dr = rotl(dr, 10) | ||
7395 | ar = fn5(ar, br, cr, dr, er, m[15], 0x50a28be6, 8); cr = rotl(cr, 10) | ||
7396 | er = fn5(er, ar, br, cr, dr, m[8], 0x50a28be6, 11); br = rotl(br, 10) | ||
7397 | dr = fn5(dr, er, ar, br, cr, m[1], 0x50a28be6, 14); ar = rotl(ar, 10) | ||
7398 | cr = fn5(cr, dr, er, ar, br, m[10], 0x50a28be6, 14); er = rotl(er, 10) | ||
7399 | br = fn5(br, cr, dr, er, ar, m[3], 0x50a28be6, 12); dr = rotl(dr, 10) | ||
7400 | ar = fn5(ar, br, cr, dr, er, m[12], 0x50a28be6, 6); cr = rotl(cr, 10) | ||
7401 | |||
7402 | // M'j = 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2 | ||
7403 | // K' = 0x5c4dd124 | ||
7404 | // S'j = 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11 | ||
7405 | er = fn4(er, ar, br, cr, dr, m[6], 0x5c4dd124, 9); br = rotl(br, 10) | ||
7406 | dr = fn4(dr, er, ar, br, cr, m[11], 0x5c4dd124, 13); ar = rotl(ar, 10) | ||
7407 | cr = fn4(cr, dr, er, ar, br, m[3], 0x5c4dd124, 15); er = rotl(er, 10) | ||
7408 | br = fn4(br, cr, dr, er, ar, m[7], 0x5c4dd124, 7); dr = rotl(dr, 10) | ||
7409 | ar = fn4(ar, br, cr, dr, er, m[0], 0x5c4dd124, 12); cr = rotl(cr, 10) | ||
7410 | er = fn4(er, ar, br, cr, dr, m[13], 0x5c4dd124, 8); br = rotl(br, 10) | ||
7411 | dr = fn4(dr, er, ar, br, cr, m[5], 0x5c4dd124, 9); ar = rotl(ar, 10) | ||
7412 | cr = fn4(cr, dr, er, ar, br, m[10], 0x5c4dd124, 11); er = rotl(er, 10) | ||
7413 | br = fn4(br, cr, dr, er, ar, m[14], 0x5c4dd124, 7); dr = rotl(dr, 10) | ||
7414 | ar = fn4(ar, br, cr, dr, er, m[15], 0x5c4dd124, 7); cr = rotl(cr, 10) | ||
7415 | er = fn4(er, ar, br, cr, dr, m[8], 0x5c4dd124, 12); br = rotl(br, 10) | ||
7416 | dr = fn4(dr, er, ar, br, cr, m[12], 0x5c4dd124, 7); ar = rotl(ar, 10) | ||
7417 | cr = fn4(cr, dr, er, ar, br, m[4], 0x5c4dd124, 6); er = rotl(er, 10) | ||
7418 | br = fn4(br, cr, dr, er, ar, m[9], 0x5c4dd124, 15); dr = rotl(dr, 10) | ||
7419 | ar = fn4(ar, br, cr, dr, er, m[1], 0x5c4dd124, 13); cr = rotl(cr, 10) | ||
7420 | er = fn4(er, ar, br, cr, dr, m[2], 0x5c4dd124, 11); br = rotl(br, 10) | ||
7421 | |||
7422 | // M'j = 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13 | ||
7423 | // K' = 0x6d703ef3 | ||
7424 | // S'j = 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5 | ||
7425 | dr = fn3(dr, er, ar, br, cr, m[15], 0x6d703ef3, 9); ar = rotl(ar, 10) | ||
7426 | cr = fn3(cr, dr, er, ar, br, m[5], 0x6d703ef3, 7); er = rotl(er, 10) | ||
7427 | br = fn3(br, cr, dr, er, ar, m[1], 0x6d703ef3, 15); dr = rotl(dr, 10) | ||
7428 | ar = fn3(ar, br, cr, dr, er, m[3], 0x6d703ef3, 11); cr = rotl(cr, 10) | ||
7429 | er = fn3(er, ar, br, cr, dr, m[7], 0x6d703ef3, 8); br = rotl(br, 10) | ||
7430 | dr = fn3(dr, er, ar, br, cr, m[14], 0x6d703ef3, 6); ar = rotl(ar, 10) | ||
7431 | cr = fn3(cr, dr, er, ar, br, m[6], 0x6d703ef3, 6); er = rotl(er, 10) | ||
7432 | br = fn3(br, cr, dr, er, ar, m[9], 0x6d703ef3, 14); dr = rotl(dr, 10) | ||
7433 | ar = fn3(ar, br, cr, dr, er, m[11], 0x6d703ef3, 12); cr = rotl(cr, 10) | ||
7434 | er = fn3(er, ar, br, cr, dr, m[8], 0x6d703ef3, 13); br = rotl(br, 10) | ||
7435 | dr = fn3(dr, er, ar, br, cr, m[12], 0x6d703ef3, 5); ar = rotl(ar, 10) | ||
7436 | cr = fn3(cr, dr, er, ar, br, m[2], 0x6d703ef3, 14); er = rotl(er, 10) | ||
7437 | br = fn3(br, cr, dr, er, ar, m[10], 0x6d703ef3, 13); dr = rotl(dr, 10) | ||
7438 | ar = fn3(ar, br, cr, dr, er, m[0], 0x6d703ef3, 13); cr = rotl(cr, 10) | ||
7439 | er = fn3(er, ar, br, cr, dr, m[4], 0x6d703ef3, 7); br = rotl(br, 10) | ||
7440 | dr = fn3(dr, er, ar, br, cr, m[13], 0x6d703ef3, 5); ar = rotl(ar, 10) | ||
7441 | |||
7442 | // M'j = 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14 | ||
7443 | // K' = 0x7a6d76e9 | ||
7444 | // S'j = 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8 | ||
7445 | cr = fn2(cr, dr, er, ar, br, m[8], 0x7a6d76e9, 15); er = rotl(er, 10) | ||
7446 | br = fn2(br, cr, dr, er, ar, m[6], 0x7a6d76e9, 5); dr = rotl(dr, 10) | ||
7447 | ar = fn2(ar, br, cr, dr, er, m[4], 0x7a6d76e9, 8); cr = rotl(cr, 10) | ||
7448 | er = fn2(er, ar, br, cr, dr, m[1], 0x7a6d76e9, 11); br = rotl(br, 10) | ||
7449 | dr = fn2(dr, er, ar, br, cr, m[3], 0x7a6d76e9, 14); ar = rotl(ar, 10) | ||
7450 | cr = fn2(cr, dr, er, ar, br, m[11], 0x7a6d76e9, 14); er = rotl(er, 10) | ||
7451 | br = fn2(br, cr, dr, er, ar, m[15], 0x7a6d76e9, 6); dr = rotl(dr, 10) | ||
7452 | ar = fn2(ar, br, cr, dr, er, m[0], 0x7a6d76e9, 14); cr = rotl(cr, 10) | ||
7453 | er = fn2(er, ar, br, cr, dr, m[5], 0x7a6d76e9, 6); br = rotl(br, 10) | ||
7454 | dr = fn2(dr, er, ar, br, cr, m[12], 0x7a6d76e9, 9); ar = rotl(ar, 10) | ||
7455 | cr = fn2(cr, dr, er, ar, br, m[2], 0x7a6d76e9, 12); er = rotl(er, 10) | ||
7456 | br = fn2(br, cr, dr, er, ar, m[13], 0x7a6d76e9, 9); dr = rotl(dr, 10) | ||
7457 | ar = fn2(ar, br, cr, dr, er, m[9], 0x7a6d76e9, 12); cr = rotl(cr, 10) | ||
7458 | er = fn2(er, ar, br, cr, dr, m[7], 0x7a6d76e9, 5); br = rotl(br, 10) | ||
7459 | dr = fn2(dr, er, ar, br, cr, m[10], 0x7a6d76e9, 15); ar = rotl(ar, 10) | ||
7460 | cr = fn2(cr, dr, er, ar, br, m[14], 0x7a6d76e9, 8); er = rotl(er, 10) | ||
7461 | |||
7462 | // M'j = 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11 | ||
7463 | // K' = 0x00000000 | ||
7464 | // S'j = 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 | ||
7465 | br = fn1(br, cr, dr, er, ar, m[12], 0x00000000, 8); dr = rotl(dr, 10) | ||
7466 | ar = fn1(ar, br, cr, dr, er, m[15], 0x00000000, 5); cr = rotl(cr, 10) | ||
7467 | er = fn1(er, ar, br, cr, dr, m[10], 0x00000000, 12); br = rotl(br, 10) | ||
7468 | dr = fn1(dr, er, ar, br, cr, m[4], 0x00000000, 9); ar = rotl(ar, 10) | ||
7469 | cr = fn1(cr, dr, er, ar, br, m[1], 0x00000000, 12); er = rotl(er, 10) | ||
7470 | br = fn1(br, cr, dr, er, ar, m[5], 0x00000000, 5); dr = rotl(dr, 10) | ||
7471 | ar = fn1(ar, br, cr, dr, er, m[8], 0x00000000, 14); cr = rotl(cr, 10) | ||
7472 | er = fn1(er, ar, br, cr, dr, m[7], 0x00000000, 6); br = rotl(br, 10) | ||
7473 | dr = fn1(dr, er, ar, br, cr, m[6], 0x00000000, 8); ar = rotl(ar, 10) | ||
7474 | cr = fn1(cr, dr, er, ar, br, m[2], 0x00000000, 13); er = rotl(er, 10) | ||
7475 | br = fn1(br, cr, dr, er, ar, m[13], 0x00000000, 6); dr = rotl(dr, 10) | ||
7476 | ar = fn1(ar, br, cr, dr, er, m[14], 0x00000000, 5); cr = rotl(cr, 10) | ||
7477 | er = fn1(er, ar, br, cr, dr, m[0], 0x00000000, 15); br = rotl(br, 10) | ||
7478 | dr = fn1(dr, er, ar, br, cr, m[3], 0x00000000, 13); ar = rotl(ar, 10) | ||
7479 | cr = fn1(cr, dr, er, ar, br, m[9], 0x00000000, 11); er = rotl(er, 10) | ||
7480 | br = fn1(br, cr, dr, er, ar, m[11], 0x00000000, 11); dr = rotl(dr, 10) | ||
7481 | |||
7482 | // change state | ||
7483 | var t = (this._b + cl + dr) | 0 | ||
7484 | this._b = (this._c + dl + er) | 0 | ||
7485 | this._c = (this._d + el + ar) | 0 | ||
7486 | this._d = (this._e + al + br) | 0 | ||
7487 | this._e = (this._a + bl + cr) | 0 | ||
7488 | this._a = t | ||
7489 | } | ||
7490 | |||
7491 | RIPEMD160.prototype._digest = function () { | ||
7492 | // create padding and handle blocks | ||
7493 | this._block[this._blockOffset++] = 0x80 | ||
7494 | if (this._blockOffset > 56) { | ||
7495 | this._block.fill(0, this._blockOffset, 64) | ||
7496 | this._update() | ||
7497 | this._blockOffset = 0 | ||
7498 | } | ||
7499 | |||
7500 | this._block.fill(0, this._blockOffset, 56) | ||
7501 | this._block.writeUInt32LE(this._length[0], 56) | ||
7502 | this._block.writeUInt32LE(this._length[1], 60) | ||
7503 | this._update() | ||
7504 | |||
7505 | // produce result | ||
7506 | var buffer = new Buffer(20) | ||
7507 | buffer.writeInt32LE(this._a, 0) | ||
7508 | buffer.writeInt32LE(this._b, 4) | ||
7509 | buffer.writeInt32LE(this._c, 8) | ||
7510 | buffer.writeInt32LE(this._d, 12) | ||
7511 | buffer.writeInt32LE(this._e, 16) | ||
7512 | return buffer | ||
7513 | } | ||
7514 | |||
7515 | function rotl (x, n) { | ||
7516 | return (x << n) | (x >>> (32 - n)) | ||
7517 | } | ||
7518 | |||
7519 | function fn1 (a, b, c, d, e, m, k, s) { | ||
7520 | return (rotl((a + (b ^ c ^ d) + m + k) | 0, s) + e) | 0 | ||
7521 | } | ||
7522 | |||
7523 | function fn2 (a, b, c, d, e, m, k, s) { | ||
7524 | return (rotl((a + ((b & c) | ((~b) & d)) + m + k) | 0, s) + e) | 0 | ||
7525 | } | ||
7526 | |||
7527 | function fn3 (a, b, c, d, e, m, k, s) { | ||
7528 | return (rotl((a + ((b | (~c)) ^ d) + m + k) | 0, s) + e) | 0 | ||
7529 | } | ||
7530 | |||
7531 | function fn4 (a, b, c, d, e, m, k, s) { | ||
7532 | return (rotl((a + ((b & d) | (c & (~d))) + m + k) | 0, s) + e) | 0 | ||
7533 | } | ||
7534 | |||
7535 | function fn5 (a, b, c, d, e, m, k, s) { | ||
7536 | return (rotl((a + (b ^ (c | (~d))) + m + k) | 0, s) + e) | 0 | ||
7537 | } | ||
7538 | |||
7539 | module.exports = RIPEMD160 | ||
7540 | |||
7541 | }).call(this,require("buffer").Buffer) | ||
7542 | },{"buffer":8,"hash-base":19,"inherits":21}],40:[function(require,module,exports){ | ||
7543 | /* eslint-disable node/no-deprecated-api */ | ||
7544 | var buffer = require('buffer') | ||
7545 | var Buffer = buffer.Buffer | ||
7546 | |||
7547 | // alternative to using Object.keys for old browsers | ||
7548 | function copyProps (src, dst) { | ||
7549 | for (var key in src) { | ||
7550 | dst[key] = src[key] | ||
7551 | } | ||
7552 | } | ||
7553 | if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) { | ||
7554 | module.exports = buffer | ||
7555 | } else { | ||
7556 | // Copy properties from require('buffer') | ||
7557 | copyProps(buffer, exports) | ||
7558 | exports.Buffer = SafeBuffer | ||
7559 | } | ||
7560 | |||
7561 | function SafeBuffer (arg, encodingOrOffset, length) { | ||
7562 | return Buffer(arg, encodingOrOffset, length) | ||
7563 | } | ||
7564 | |||
7565 | // Copy static methods from Buffer | ||
7566 | copyProps(Buffer, SafeBuffer) | ||
7567 | |||
7568 | SafeBuffer.from = function (arg, encodingOrOffset, length) { | ||
7569 | if (typeof arg === 'number') { | ||
7570 | throw new TypeError('Argument must not be a number') | ||
7571 | } | ||
7572 | return Buffer(arg, encodingOrOffset, length) | ||
7573 | } | ||
7574 | |||
7575 | SafeBuffer.alloc = function (size, fill, encoding) { | ||
7576 | if (typeof size !== 'number') { | ||
7577 | throw new TypeError('Argument must be a number') | ||
7578 | } | ||
7579 | var buf = Buffer(size) | ||
7580 | if (fill !== undefined) { | ||
7581 | if (typeof encoding === 'string') { | ||
7582 | buf.fill(fill, encoding) | ||
7583 | } else { | ||
7584 | buf.fill(fill) | ||
7585 | } | ||
7586 | } else { | ||
7587 | buf.fill(0) | ||
7588 | } | ||
7589 | return buf | ||
7590 | } | ||
7591 | |||
7592 | SafeBuffer.allocUnsafe = function (size) { | ||
7593 | if (typeof size !== 'number') { | ||
7594 | throw new TypeError('Argument must be a number') | ||
7595 | } | ||
7596 | return Buffer(size) | ||
7597 | } | ||
7598 | |||
7599 | SafeBuffer.allocUnsafeSlow = function (size) { | ||
7600 | if (typeof size !== 'number') { | ||
7601 | throw new TypeError('Argument must be a number') | ||
7602 | } | ||
7603 | return buffer.SlowBuffer(size) | ||
7604 | } | ||
7605 | |||
7606 | },{"buffer":8}],41:[function(require,module,exports){ | ||
7607 | var Buffer = require('safe-buffer').Buffer | ||
7608 | |||
7609 | // prototype class for hash functions | ||
7610 | function Hash (blockSize, finalSize) { | ||
7611 | this._block = Buffer.alloc(blockSize) | ||
7612 | this._finalSize = finalSize | ||
7613 | this._blockSize = blockSize | ||
7614 | this._len = 0 | ||
7615 | } | ||
7616 | |||
7617 | Hash.prototype.update = function (data, enc) { | ||
7618 | if (typeof data === 'string') { | ||
7619 | enc = enc || 'utf8' | ||
7620 | data = Buffer.from(data, enc) | ||
7621 | } | ||
7622 | |||
7623 | var block = this._block | ||
7624 | var blockSize = this._blockSize | ||
7625 | var length = data.length | ||
7626 | var accum = this._len | ||
7627 | |||
7628 | for (var offset = 0; offset < length;) { | ||
7629 | var assigned = accum % blockSize | ||
7630 | var remainder = Math.min(length - offset, blockSize - assigned) | ||
7631 | |||
7632 | for (var i = 0; i < remainder; i++) { | ||
7633 | block[assigned + i] = data[offset + i] | ||
7634 | } | ||
7635 | |||
7636 | accum += remainder | ||
7637 | offset += remainder | ||
7638 | |||
7639 | if ((accum % blockSize) === 0) { | ||
7640 | this._update(block) | ||
7641 | } | ||
7642 | } | ||
7643 | |||
7644 | this._len += length | ||
7645 | return this | ||
7646 | } | ||
7647 | |||
7648 | Hash.prototype.digest = function (enc) { | ||
7649 | var rem = this._len % this._blockSize | ||
7650 | |||
7651 | this._block[rem] = 0x80 | ||
7652 | |||
7653 | // zero (rem + 1) trailing bits, where (rem + 1) is the smallest | ||
7654 | // non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize | ||
7655 | this._block.fill(0, rem + 1) | ||
7656 | |||
7657 | if (rem >= this._finalSize) { | ||
7658 | this._update(this._block) | ||
7659 | this._block.fill(0) | ||
7660 | } | ||
7661 | |||
7662 | var bits = this._len * 8 | ||
7663 | |||
7664 | // uint32 | ||
7665 | if (bits <= 0xffffffff) { | ||
7666 | this._block.writeUInt32BE(bits, this._blockSize - 4) | ||
7667 | |||
7668 | // uint64 | ||
7669 | } else { | ||
7670 | var lowBits = bits & 0xffffffff | ||
7671 | var highBits = (bits - lowBits) / 0x100000000 | ||
7672 | |||
7673 | this._block.writeUInt32BE(highBits, this._blockSize - 8) | ||
7674 | this._block.writeUInt32BE(lowBits, this._blockSize - 4) | ||
7675 | } | ||
7676 | |||
7677 | this._update(this._block) | ||
7678 | var hash = this._hash() | ||
7679 | |||
7680 | return enc ? hash.toString(enc) : hash | ||
7681 | } | ||
7682 | |||
7683 | Hash.prototype._update = function () { | ||
7684 | throw new Error('_update must be implemented by subclass') | ||
7685 | } | ||
7686 | |||
7687 | module.exports = Hash | ||
7688 | |||
7689 | },{"safe-buffer":40}],42:[function(require,module,exports){ | ||
7690 | var exports = module.exports = function SHA (algorithm) { | ||
7691 | algorithm = algorithm.toLowerCase() | ||
7692 | |||
7693 | var Algorithm = exports[algorithm] | ||
7694 | if (!Algorithm) throw new Error(algorithm + ' is not supported (we accept pull requests)') | ||
7695 | |||
7696 | return new Algorithm() | ||
7697 | } | ||
7698 | |||
7699 | exports.sha = require('./sha') | ||
7700 | exports.sha1 = require('./sha1') | ||
7701 | exports.sha224 = require('./sha224') | ||
7702 | exports.sha256 = require('./sha256') | ||
7703 | exports.sha384 = require('./sha384') | ||
7704 | exports.sha512 = require('./sha512') | ||
7705 | |||
7706 | },{"./sha":43,"./sha1":44,"./sha224":45,"./sha256":46,"./sha384":47,"./sha512":48}],43:[function(require,module,exports){ | ||
7707 | /* | ||
7708 | * A JavaScript implementation of the Secure Hash Algorithm, SHA-0, as defined | ||
7709 | * in FIPS PUB 180-1 | ||
7710 | * This source code is derived from sha1.js of the same repository. | ||
7711 | * The difference between SHA-0 and SHA-1 is just a bitwise rotate left | ||
7712 | * operation was added. | ||
7713 | */ | ||
7714 | |||
7715 | var inherits = require('inherits') | ||
7716 | var Hash = require('./hash') | ||
7717 | var Buffer = require('safe-buffer').Buffer | ||
7718 | |||
7719 | var K = [ | ||
7720 | 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0 | ||
7721 | ] | ||
7722 | |||
7723 | var W = new Array(80) | ||
7724 | |||
7725 | function Sha () { | ||
7726 | this.init() | ||
7727 | this._w = W | ||
7728 | |||
7729 | Hash.call(this, 64, 56) | ||
7730 | } | ||
7731 | |||
7732 | inherits(Sha, Hash) | ||
7733 | |||
7734 | Sha.prototype.init = function () { | ||
7735 | this._a = 0x67452301 | ||
7736 | this._b = 0xefcdab89 | ||
7737 | this._c = 0x98badcfe | ||
7738 | this._d = 0x10325476 | ||
7739 | this._e = 0xc3d2e1f0 | ||
7740 | |||
7741 | return this | ||
7742 | } | ||
7743 | |||
7744 | function rotl5 (num) { | ||
7745 | return (num << 5) | (num >>> 27) | ||
7746 | } | ||
7747 | |||
7748 | function rotl30 (num) { | ||
7749 | return (num << 30) | (num >>> 2) | ||
7750 | } | ||
7751 | |||
7752 | function ft (s, b, c, d) { | ||
7753 | if (s === 0) return (b & c) | ((~b) & d) | ||
7754 | if (s === 2) return (b & c) | (b & d) | (c & d) | ||
7755 | return b ^ c ^ d | ||
7756 | } | ||
7757 | |||
7758 | Sha.prototype._update = function (M) { | ||
7759 | var W = this._w | ||
7760 | |||
7761 | var a = this._a | 0 | ||
7762 | var b = this._b | 0 | ||
7763 | var c = this._c | 0 | ||
7764 | var d = this._d | 0 | ||
7765 | var e = this._e | 0 | ||
7766 | |||
7767 | for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4) | ||
7768 | for (; i < 80; ++i) W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16] | ||
7769 | |||
7770 | for (var j = 0; j < 80; ++j) { | ||
7771 | var s = ~~(j / 20) | ||
7772 | var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0 | ||
7773 | |||
7774 | e = d | ||
7775 | d = c | ||
7776 | c = rotl30(b) | ||
7777 | b = a | ||
7778 | a = t | ||
7779 | } | ||
7780 | |||
7781 | this._a = (a + this._a) | 0 | ||
7782 | this._b = (b + this._b) | 0 | ||
7783 | this._c = (c + this._c) | 0 | ||
7784 | this._d = (d + this._d) | 0 | ||
7785 | this._e = (e + this._e) | 0 | ||
7786 | } | ||
7787 | |||
7788 | Sha.prototype._hash = function () { | ||
7789 | var H = Buffer.allocUnsafe(20) | ||
7790 | |||
7791 | H.writeInt32BE(this._a | 0, 0) | ||
7792 | H.writeInt32BE(this._b | 0, 4) | ||
7793 | H.writeInt32BE(this._c | 0, 8) | ||
7794 | H.writeInt32BE(this._d | 0, 12) | ||
7795 | H.writeInt32BE(this._e | 0, 16) | ||
7796 | |||
7797 | return H | ||
7798 | } | ||
7799 | |||
7800 | module.exports = Sha | ||
7801 | |||
7802 | },{"./hash":41,"inherits":21,"safe-buffer":40}],44:[function(require,module,exports){ | ||
7803 | /* | ||
7804 | * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined | ||
7805 | * in FIPS PUB 180-1 | ||
7806 | * Version 2.1a Copyright Paul Johnston 2000 - 2002. | ||
7807 | * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet | ||
7808 | * Distributed under the BSD License | ||
7809 | * See http://pajhome.org.uk/crypt/md5 for details. | ||
7810 | */ | ||
7811 | |||
7812 | var inherits = require('inherits') | ||
7813 | var Hash = require('./hash') | ||
7814 | var Buffer = require('safe-buffer').Buffer | ||
7815 | |||
7816 | var K = [ | ||
7817 | 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0 | ||
7818 | ] | ||
7819 | |||
7820 | var W = new Array(80) | ||
7821 | |||
7822 | function Sha1 () { | ||
7823 | this.init() | ||
7824 | this._w = W | ||
7825 | |||
7826 | Hash.call(this, 64, 56) | ||
7827 | } | ||
7828 | |||
7829 | inherits(Sha1, Hash) | ||
7830 | |||
7831 | Sha1.prototype.init = function () { | ||
7832 | this._a = 0x67452301 | ||
7833 | this._b = 0xefcdab89 | ||
7834 | this._c = 0x98badcfe | ||
7835 | this._d = 0x10325476 | ||
7836 | this._e = 0xc3d2e1f0 | ||
7837 | |||
7838 | return this | ||
7839 | } | ||
7840 | |||
7841 | function rotl1 (num) { | ||
7842 | return (num << 1) | (num >>> 31) | ||
7843 | } | ||
7844 | |||
7845 | function rotl5 (num) { | ||
7846 | return (num << 5) | (num >>> 27) | ||
7847 | } | ||
7848 | |||
7849 | function rotl30 (num) { | ||
7850 | return (num << 30) | (num >>> 2) | ||
7851 | } | ||
7852 | |||
7853 | function ft (s, b, c, d) { | ||
7854 | if (s === 0) return (b & c) | ((~b) & d) | ||
7855 | if (s === 2) return (b & c) | (b & d) | (c & d) | ||
7856 | return b ^ c ^ d | ||
7857 | } | ||
7858 | |||
7859 | Sha1.prototype._update = function (M) { | ||
7860 | var W = this._w | ||
7861 | |||
7862 | var a = this._a | 0 | ||
7863 | var b = this._b | 0 | ||
7864 | var c = this._c | 0 | ||
7865 | var d = this._d | 0 | ||
7866 | var e = this._e | 0 | ||
7867 | |||
7868 | for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4) | ||
7869 | for (; i < 80; ++i) W[i] = rotl1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]) | ||
7870 | |||
7871 | for (var j = 0; j < 80; ++j) { | ||
7872 | var s = ~~(j / 20) | ||
7873 | var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0 | ||
7874 | |||
7875 | e = d | ||
7876 | d = c | ||
7877 | c = rotl30(b) | ||
7878 | b = a | ||
7879 | a = t | ||
7880 | } | ||
7881 | |||
7882 | this._a = (a + this._a) | 0 | ||
7883 | this._b = (b + this._b) | 0 | ||
7884 | this._c = (c + this._c) | 0 | ||
7885 | this._d = (d + this._d) | 0 | ||
7886 | this._e = (e + this._e) | 0 | ||
7887 | } | ||
7888 | |||
7889 | Sha1.prototype._hash = function () { | ||
7890 | var H = Buffer.allocUnsafe(20) | ||
7891 | |||
7892 | H.writeInt32BE(this._a | 0, 0) | ||
7893 | H.writeInt32BE(this._b | 0, 4) | ||
7894 | H.writeInt32BE(this._c | 0, 8) | ||
7895 | H.writeInt32BE(this._d | 0, 12) | ||
7896 | H.writeInt32BE(this._e | 0, 16) | ||
7897 | |||
7898 | return H | ||
7899 | } | ||
7900 | |||
7901 | module.exports = Sha1 | ||
7902 | |||
7903 | },{"./hash":41,"inherits":21,"safe-buffer":40}],45:[function(require,module,exports){ | ||
7904 | /** | ||
7905 | * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined | ||
7906 | * in FIPS 180-2 | ||
7907 | * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009. | ||
7908 | * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet | ||
7909 | * | ||
7910 | */ | ||
7911 | |||
7912 | var inherits = require('inherits') | ||
7913 | var Sha256 = require('./sha256') | ||
7914 | var Hash = require('./hash') | ||
7915 | var Buffer = require('safe-buffer').Buffer | ||
7916 | |||
7917 | var W = new Array(64) | ||
7918 | |||
7919 | function Sha224 () { | ||
7920 | this.init() | ||
7921 | |||
7922 | this._w = W // new Array(64) | ||
7923 | |||
7924 | Hash.call(this, 64, 56) | ||
7925 | } | ||
7926 | |||
7927 | inherits(Sha224, Sha256) | ||
7928 | |||
7929 | Sha224.prototype.init = function () { | ||
7930 | this._a = 0xc1059ed8 | ||
7931 | this._b = 0x367cd507 | ||
7932 | this._c = 0x3070dd17 | ||
7933 | this._d = 0xf70e5939 | ||
7934 | this._e = 0xffc00b31 | ||
7935 | this._f = 0x68581511 | ||
7936 | this._g = 0x64f98fa7 | ||
7937 | this._h = 0xbefa4fa4 | ||
7938 | |||
7939 | return this | ||
7940 | } | ||
7941 | |||
7942 | Sha224.prototype._hash = function () { | ||
7943 | var H = Buffer.allocUnsafe(28) | ||
7944 | |||
7945 | H.writeInt32BE(this._a, 0) | ||
7946 | H.writeInt32BE(this._b, 4) | ||
7947 | H.writeInt32BE(this._c, 8) | ||
7948 | H.writeInt32BE(this._d, 12) | ||
7949 | H.writeInt32BE(this._e, 16) | ||
7950 | H.writeInt32BE(this._f, 20) | ||
7951 | H.writeInt32BE(this._g, 24) | ||
7952 | |||
7953 | return H | ||
7954 | } | ||
7955 | |||
7956 | module.exports = Sha224 | ||
7957 | |||
7958 | },{"./hash":41,"./sha256":46,"inherits":21,"safe-buffer":40}],46:[function(require,module,exports){ | ||
7959 | /** | ||
7960 | * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined | ||
7961 | * in FIPS 180-2 | ||
7962 | * Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009. | ||
7963 | * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet | ||
7964 | * | ||
7965 | */ | ||
7966 | |||
7967 | var inherits = require('inherits') | ||
7968 | var Hash = require('./hash') | ||
7969 | var Buffer = require('safe-buffer').Buffer | ||
7970 | |||
7971 | var K = [ | ||
7972 | 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, | ||
7973 | 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, | ||
7974 | 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, | ||
7975 | 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, | ||
7976 | 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, | ||
7977 | 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, | ||
7978 | 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, | ||
7979 | 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, | ||
7980 | 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, | ||
7981 | 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, | ||
7982 | 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, | ||
7983 | 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, | ||
7984 | 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, | ||
7985 | 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, | ||
7986 | 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, | ||
7987 | 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2 | ||
7988 | ] | ||
7989 | |||
7990 | var W = new Array(64) | ||
7991 | |||
7992 | function Sha256 () { | ||
7993 | this.init() | ||
7994 | |||
7995 | this._w = W // new Array(64) | ||
7996 | |||
7997 | Hash.call(this, 64, 56) | ||
7998 | } | ||
7999 | |||
8000 | inherits(Sha256, Hash) | ||
8001 | |||
8002 | Sha256.prototype.init = function () { | ||
8003 | this._a = 0x6a09e667 | ||
8004 | this._b = 0xbb67ae85 | ||
8005 | this._c = 0x3c6ef372 | ||
8006 | this._d = 0xa54ff53a | ||
8007 | this._e = 0x510e527f | ||
8008 | this._f = 0x9b05688c | ||
8009 | this._g = 0x1f83d9ab | ||
8010 | this._h = 0x5be0cd19 | ||
8011 | |||
8012 | return this | ||
8013 | } | ||
8014 | |||
8015 | function ch (x, y, z) { | ||
8016 | return z ^ (x & (y ^ z)) | ||
8017 | } | ||
8018 | |||
8019 | function maj (x, y, z) { | ||
8020 | return (x & y) | (z & (x | y)) | ||
8021 | } | ||
8022 | |||
8023 | function sigma0 (x) { | ||
8024 | return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10) | ||
8025 | } | ||
8026 | |||
8027 | function sigma1 (x) { | ||
8028 | return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7) | ||
8029 | } | ||
8030 | |||
8031 | function gamma0 (x) { | ||
8032 | return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3) | ||
8033 | } | ||
8034 | |||
8035 | function gamma1 (x) { | ||
8036 | return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10) | ||
8037 | } | ||
8038 | |||
8039 | Sha256.prototype._update = function (M) { | ||
8040 | var W = this._w | ||
8041 | |||
8042 | var a = this._a | 0 | ||
8043 | var b = this._b | 0 | ||
8044 | var c = this._c | 0 | ||
8045 | var d = this._d | 0 | ||
8046 | var e = this._e | 0 | ||
8047 | var f = this._f | 0 | ||
8048 | var g = this._g | 0 | ||
8049 | var h = this._h | 0 | ||
8050 | |||
8051 | for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4) | ||
8052 | for (; i < 64; ++i) W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0 | ||
8053 | |||
8054 | for (var j = 0; j < 64; ++j) { | ||
8055 | var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + W[j]) | 0 | ||
8056 | var T2 = (sigma0(a) + maj(a, b, c)) | 0 | ||
8057 | |||
8058 | h = g | ||
8059 | g = f | ||
8060 | f = e | ||
8061 | e = (d + T1) | 0 | ||
8062 | d = c | ||
8063 | c = b | ||
8064 | b = a | ||
8065 | a = (T1 + T2) | 0 | ||
8066 | } | ||
8067 | |||
8068 | this._a = (a + this._a) | 0 | ||
8069 | this._b = (b + this._b) | 0 | ||
8070 | this._c = (c + this._c) | 0 | ||
8071 | this._d = (d + this._d) | 0 | ||
8072 | this._e = (e + this._e) | 0 | ||
8073 | this._f = (f + this._f) | 0 | ||
8074 | this._g = (g + this._g) | 0 | ||
8075 | this._h = (h + this._h) | 0 | ||
8076 | } | ||
8077 | |||
8078 | Sha256.prototype._hash = function () { | ||
8079 | var H = Buffer.allocUnsafe(32) | ||
8080 | |||
8081 | H.writeInt32BE(this._a, 0) | ||
8082 | H.writeInt32BE(this._b, 4) | ||
8083 | H.writeInt32BE(this._c, 8) | ||
8084 | H.writeInt32BE(this._d, 12) | ||
8085 | H.writeInt32BE(this._e, 16) | ||
8086 | H.writeInt32BE(this._f, 20) | ||
8087 | H.writeInt32BE(this._g, 24) | ||
8088 | H.writeInt32BE(this._h, 28) | ||
8089 | |||
8090 | return H | ||
8091 | } | ||
8092 | |||
8093 | module.exports = Sha256 | ||
8094 | |||
8095 | },{"./hash":41,"inherits":21,"safe-buffer":40}],47:[function(require,module,exports){ | ||
8096 | var inherits = require('inherits') | ||
8097 | var SHA512 = require('./sha512') | ||
8098 | var Hash = require('./hash') | ||
8099 | var Buffer = require('safe-buffer').Buffer | ||
8100 | |||
8101 | var W = new Array(160) | ||
8102 | |||
8103 | function Sha384 () { | ||
8104 | this.init() | ||
8105 | this._w = W | ||
8106 | |||
8107 | Hash.call(this, 128, 112) | ||
8108 | } | ||
8109 | |||
8110 | inherits(Sha384, SHA512) | ||
8111 | |||
8112 | Sha384.prototype.init = function () { | ||
8113 | this._ah = 0xcbbb9d5d | ||
8114 | this._bh = 0x629a292a | ||
8115 | this._ch = 0x9159015a | ||
8116 | this._dh = 0x152fecd8 | ||
8117 | this._eh = 0x67332667 | ||
8118 | this._fh = 0x8eb44a87 | ||
8119 | this._gh = 0xdb0c2e0d | ||
8120 | this._hh = 0x47b5481d | ||
8121 | |||
8122 | this._al = 0xc1059ed8 | ||
8123 | this._bl = 0x367cd507 | ||
8124 | this._cl = 0x3070dd17 | ||
8125 | this._dl = 0xf70e5939 | ||
8126 | this._el = 0xffc00b31 | ||
8127 | this._fl = 0x68581511 | ||
8128 | this._gl = 0x64f98fa7 | ||
8129 | this._hl = 0xbefa4fa4 | ||
8130 | |||
8131 | return this | ||
8132 | } | ||
8133 | |||
8134 | Sha384.prototype._hash = function () { | ||
8135 | var H = Buffer.allocUnsafe(48) | ||
8136 | |||
8137 | function writeInt64BE (h, l, offset) { | ||
8138 | H.writeInt32BE(h, offset) | ||
8139 | H.writeInt32BE(l, offset + 4) | ||
8140 | } | ||
8141 | |||
8142 | writeInt64BE(this._ah, this._al, 0) | ||
8143 | writeInt64BE(this._bh, this._bl, 8) | ||
8144 | writeInt64BE(this._ch, this._cl, 16) | ||
8145 | writeInt64BE(this._dh, this._dl, 24) | ||
8146 | writeInt64BE(this._eh, this._el, 32) | ||
8147 | writeInt64BE(this._fh, this._fl, 40) | ||
8148 | |||
8149 | return H | ||
8150 | } | ||
8151 | |||
8152 | module.exports = Sha384 | ||
8153 | |||
8154 | },{"./hash":41,"./sha512":48,"inherits":21,"safe-buffer":40}],48:[function(require,module,exports){ | ||
8155 | var inherits = require('inherits') | ||
8156 | var Hash = require('./hash') | ||
8157 | var Buffer = require('safe-buffer').Buffer | ||
8158 | |||
8159 | var K = [ | ||
8160 | 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd, | ||
8161 | 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc, | ||
8162 | 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019, | ||
8163 | 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118, | ||
8164 | 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe, | ||
8165 | 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2, | ||
8166 | 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1, | ||
8167 | 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694, | ||
8168 | 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3, | ||
8169 | 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65, | ||
8170 | 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483, | ||
8171 | 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5, | ||
8172 | 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210, | ||
8173 | 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4, | ||
8174 | 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725, | ||
8175 | 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70, | ||
8176 | 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926, | ||
8177 | 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df, | ||
8178 | 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8, | ||
8179 | 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b, | ||
8180 | 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001, | ||
8181 | 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30, | ||
8182 | 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910, | ||
8183 | 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8, | ||
8184 | 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53, | ||
8185 | 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8, | ||
8186 | 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb, | ||
8187 | 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3, | ||
8188 | 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60, | ||
8189 | 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec, | ||
8190 | 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9, | ||
8191 | 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b, | ||
8192 | 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207, | ||
8193 | 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178, | ||
8194 | 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6, | ||
8195 | 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b, | ||
8196 | 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493, | ||
8197 | 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c, | ||
8198 | 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a, | ||
8199 | 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817 | ||
8200 | ] | ||
8201 | |||
8202 | var W = new Array(160) | ||
8203 | |||
8204 | function Sha512 () { | ||
8205 | this.init() | ||
8206 | this._w = W | ||
8207 | |||
8208 | Hash.call(this, 128, 112) | ||
8209 | } | ||
8210 | |||
8211 | inherits(Sha512, Hash) | ||
8212 | |||
8213 | Sha512.prototype.init = function () { | ||
8214 | this._ah = 0x6a09e667 | ||
8215 | this._bh = 0xbb67ae85 | ||
8216 | this._ch = 0x3c6ef372 | ||
8217 | this._dh = 0xa54ff53a | ||
8218 | this._eh = 0x510e527f | ||
8219 | this._fh = 0x9b05688c | ||
8220 | this._gh = 0x1f83d9ab | ||
8221 | this._hh = 0x5be0cd19 | ||
8222 | |||
8223 | this._al = 0xf3bcc908 | ||
8224 | this._bl = 0x84caa73b | ||
8225 | this._cl = 0xfe94f82b | ||
8226 | this._dl = 0x5f1d36f1 | ||
8227 | this._el = 0xade682d1 | ||
8228 | this._fl = 0x2b3e6c1f | ||
8229 | this._gl = 0xfb41bd6b | ||
8230 | this._hl = 0x137e2179 | ||
8231 | |||
8232 | return this | ||
8233 | } | ||
8234 | |||
8235 | function Ch (x, y, z) { | ||
8236 | return z ^ (x & (y ^ z)) | ||
8237 | } | ||
8238 | |||
8239 | function maj (x, y, z) { | ||
8240 | return (x & y) | (z & (x | y)) | ||
8241 | } | ||
8242 | |||
8243 | function sigma0 (x, xl) { | ||
8244 | return (x >>> 28 | xl << 4) ^ (xl >>> 2 | x << 30) ^ (xl >>> 7 | x << 25) | ||
8245 | } | ||
8246 | |||
8247 | function sigma1 (x, xl) { | ||
8248 | return (x >>> 14 | xl << 18) ^ (x >>> 18 | xl << 14) ^ (xl >>> 9 | x << 23) | ||
8249 | } | ||
8250 | |||
8251 | function Gamma0 (x, xl) { | ||
8252 | return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7) | ||
8253 | } | ||
8254 | |||
8255 | function Gamma0l (x, xl) { | ||
8256 | return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7 | xl << 25) | ||
8257 | } | ||
8258 | |||
8259 | function Gamma1 (x, xl) { | ||
8260 | return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6) | ||
8261 | } | ||
8262 | |||
8263 | function Gamma1l (x, xl) { | ||
8264 | return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6 | xl << 26) | ||
8265 | } | ||
8266 | |||
8267 | function getCarry (a, b) { | ||
8268 | return (a >>> 0) < (b >>> 0) ? 1 : 0 | ||
8269 | } | ||
8270 | |||
8271 | Sha512.prototype._update = function (M) { | ||
8272 | var W = this._w | ||
8273 | |||
8274 | var ah = this._ah | 0 | ||
8275 | var bh = this._bh | 0 | ||
8276 | var ch = this._ch | 0 | ||
8277 | var dh = this._dh | 0 | ||
8278 | var eh = this._eh | 0 | ||
8279 | var fh = this._fh | 0 | ||
8280 | var gh = this._gh | 0 | ||
8281 | var hh = this._hh | 0 | ||
8282 | |||
8283 | var al = this._al | 0 | ||
8284 | var bl = this._bl | 0 | ||
8285 | var cl = this._cl | 0 | ||
8286 | var dl = this._dl | 0 | ||
8287 | var el = this._el | 0 | ||
8288 | var fl = this._fl | 0 | ||
8289 | var gl = this._gl | 0 | ||
8290 | var hl = this._hl | 0 | ||
8291 | |||
8292 | for (var i = 0; i < 32; i += 2) { | ||
8293 | W[i] = M.readInt32BE(i * 4) | ||
8294 | W[i + 1] = M.readInt32BE(i * 4 + 4) | ||
8295 | } | ||
8296 | for (; i < 160; i += 2) { | ||
8297 | var xh = W[i - 15 * 2] | ||
8298 | var xl = W[i - 15 * 2 + 1] | ||
8299 | var gamma0 = Gamma0(xh, xl) | ||
8300 | var gamma0l = Gamma0l(xl, xh) | ||
8301 | |||
8302 | xh = W[i - 2 * 2] | ||
8303 | xl = W[i - 2 * 2 + 1] | ||
8304 | var gamma1 = Gamma1(xh, xl) | ||
8305 | var gamma1l = Gamma1l(xl, xh) | ||
8306 | |||
8307 | // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16] | ||
8308 | var Wi7h = W[i - 7 * 2] | ||
8309 | var Wi7l = W[i - 7 * 2 + 1] | ||
8310 | |||
8311 | var Wi16h = W[i - 16 * 2] | ||
8312 | var Wi16l = W[i - 16 * 2 + 1] | ||
8313 | |||
8314 | var Wil = (gamma0l + Wi7l) | 0 | ||
8315 | var Wih = (gamma0 + Wi7h + getCarry(Wil, gamma0l)) | 0 | ||
8316 | Wil = (Wil + gamma1l) | 0 | ||
8317 | Wih = (Wih + gamma1 + getCarry(Wil, gamma1l)) | 0 | ||
8318 | Wil = (Wil + Wi16l) | 0 | ||
8319 | Wih = (Wih + Wi16h + getCarry(Wil, Wi16l)) | 0 | ||
8320 | |||
8321 | W[i] = Wih | ||
8322 | W[i + 1] = Wil | ||
8323 | } | ||
8324 | |||
8325 | for (var j = 0; j < 160; j += 2) { | ||
8326 | Wih = W[j] | ||
8327 | Wil = W[j + 1] | ||
8328 | |||
8329 | var majh = maj(ah, bh, ch) | ||
8330 | var majl = maj(al, bl, cl) | ||
8331 | |||
8332 | var sigma0h = sigma0(ah, al) | ||
8333 | var sigma0l = sigma0(al, ah) | ||
8334 | var sigma1h = sigma1(eh, el) | ||
8335 | var sigma1l = sigma1(el, eh) | ||
8336 | |||
8337 | // t1 = h + sigma1 + ch + K[j] + W[j] | ||
8338 | var Kih = K[j] | ||
8339 | var Kil = K[j + 1] | ||
8340 | |||
8341 | var chh = Ch(eh, fh, gh) | ||
8342 | var chl = Ch(el, fl, gl) | ||
8343 | |||
8344 | var t1l = (hl + sigma1l) | 0 | ||
8345 | var t1h = (hh + sigma1h + getCarry(t1l, hl)) | 0 | ||
8346 | t1l = (t1l + chl) | 0 | ||
8347 | t1h = (t1h + chh + getCarry(t1l, chl)) | 0 | ||
8348 | t1l = (t1l + Kil) | 0 | ||
8349 | t1h = (t1h + Kih + getCarry(t1l, Kil)) | 0 | ||
8350 | t1l = (t1l + Wil) | 0 | ||
8351 | t1h = (t1h + Wih + getCarry(t1l, Wil)) | 0 | ||
8352 | |||
8353 | // t2 = sigma0 + maj | ||
8354 | var t2l = (sigma0l + majl) | 0 | ||
8355 | var t2h = (sigma0h + majh + getCarry(t2l, sigma0l)) | 0 | ||
8356 | |||
8357 | hh = gh | ||
8358 | hl = gl | ||
8359 | gh = fh | ||
8360 | gl = fl | ||
8361 | fh = eh | ||
8362 | fl = el | ||
8363 | el = (dl + t1l) | 0 | ||
8364 | eh = (dh + t1h + getCarry(el, dl)) | 0 | ||
8365 | dh = ch | ||
8366 | dl = cl | ||
8367 | ch = bh | ||
8368 | cl = bl | ||
8369 | bh = ah | ||
8370 | bl = al | ||
8371 | al = (t1l + t2l) | 0 | ||
8372 | ah = (t1h + t2h + getCarry(al, t1l)) | 0 | ||
8373 | } | ||
8374 | |||
8375 | this._al = (this._al + al) | 0 | ||
8376 | this._bl = (this._bl + bl) | 0 | ||
8377 | this._cl = (this._cl + cl) | 0 | ||
8378 | this._dl = (this._dl + dl) | 0 | ||
8379 | this._el = (this._el + el) | 0 | ||
8380 | this._fl = (this._fl + fl) | 0 | ||
8381 | this._gl = (this._gl + gl) | 0 | ||
8382 | this._hl = (this._hl + hl) | 0 | ||
8383 | |||
8384 | this._ah = (this._ah + ah + getCarry(this._al, al)) | 0 | ||
8385 | this._bh = (this._bh + bh + getCarry(this._bl, bl)) | 0 | ||
8386 | this._ch = (this._ch + ch + getCarry(this._cl, cl)) | 0 | ||
8387 | this._dh = (this._dh + dh + getCarry(this._dl, dl)) | 0 | ||
8388 | this._eh = (this._eh + eh + getCarry(this._el, el)) | 0 | ||
8389 | this._fh = (this._fh + fh + getCarry(this._fl, fl)) | 0 | ||
8390 | this._gh = (this._gh + gh + getCarry(this._gl, gl)) | 0 | ||
8391 | this._hh = (this._hh + hh + getCarry(this._hl, hl)) | 0 | ||
8392 | } | ||
8393 | |||
8394 | Sha512.prototype._hash = function () { | ||
8395 | var H = Buffer.allocUnsafe(64) | ||
8396 | |||
8397 | function writeInt64BE (h, l, offset) { | ||
8398 | H.writeInt32BE(h, offset) | ||
8399 | H.writeInt32BE(l, offset + 4) | ||
8400 | } | ||
8401 | |||
8402 | writeInt64BE(this._ah, this._al, 0) | ||
8403 | writeInt64BE(this._bh, this._bl, 8) | ||
8404 | writeInt64BE(this._ch, this._cl, 16) | ||
8405 | writeInt64BE(this._dh, this._dl, 24) | ||
8406 | writeInt64BE(this._eh, this._el, 32) | ||
8407 | writeInt64BE(this._fh, this._fl, 40) | ||
8408 | writeInt64BE(this._gh, this._gl, 48) | ||
8409 | writeInt64BE(this._hh, this._hl, 56) | ||
8410 | |||
8411 | return H | ||
8412 | } | ||
8413 | |||
8414 | module.exports = Sha512 | ||
8415 | |||
8416 | },{"./hash":41,"inherits":21,"safe-buffer":40}],49:[function(require,module,exports){ | ||
8417 | // Copyright Joyent, Inc. and other Node contributors. | ||
8418 | // | ||
8419 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
8420 | // copy of this software and associated documentation files (the | ||
8421 | // "Software"), to deal in the Software without restriction, including | ||
8422 | // without limitation the rights to use, copy, modify, merge, publish, | ||
8423 | // distribute, sublicense, and/or sell copies of the Software, and to permit | ||
8424 | // persons to whom the Software is furnished to do so, subject to the | ||
8425 | // following conditions: | ||
8426 | // | ||
8427 | // The above copyright notice and this permission notice shall be included | ||
8428 | // in all copies or substantial portions of the Software. | ||
8429 | // | ||
8430 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
8431 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
8432 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
8433 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
8434 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
8435 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
8436 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
8437 | |||
8438 | module.exports = Stream; | ||
8439 | |||
8440 | var EE = require('events').EventEmitter; | ||
8441 | var inherits = require('inherits'); | ||
8442 | |||
8443 | inherits(Stream, EE); | ||
8444 | Stream.Readable = require('readable-stream/readable.js'); | ||
8445 | Stream.Writable = require('readable-stream/writable.js'); | ||
8446 | Stream.Duplex = require('readable-stream/duplex.js'); | ||
8447 | Stream.Transform = require('readable-stream/transform.js'); | ||
8448 | Stream.PassThrough = require('readable-stream/passthrough.js'); | ||
8449 | |||
8450 | // Backwards-compat with node 0.4.x | ||
8451 | Stream.Stream = Stream; | ||
8452 | |||
8453 | |||
8454 | |||
8455 | // old-style streams. Note that the pipe method (the only relevant | ||
8456 | // part of this class) is overridden in the Readable class. | ||
8457 | |||
8458 | function Stream() { | ||
8459 | EE.call(this); | ||
8460 | } | ||
8461 | |||
8462 | Stream.prototype.pipe = function(dest, options) { | ||
8463 | var source = this; | ||
8464 | |||
8465 | function ondata(chunk) { | ||
8466 | if (dest.writable) { | ||
8467 | if (false === dest.write(chunk) && source.pause) { | ||
8468 | source.pause(); | ||
8469 | } | ||
8470 | } | ||
8471 | } | ||
8472 | |||
8473 | source.on('data', ondata); | ||
8474 | |||
8475 | function ondrain() { | ||
8476 | if (source.readable && source.resume) { | ||
8477 | source.resume(); | ||
8478 | } | ||
8479 | } | ||
8480 | |||
8481 | dest.on('drain', ondrain); | ||
8482 | |||
8483 | // If the 'end' option is not supplied, dest.end() will be called when | ||
8484 | // source gets the 'end' or 'close' events. Only dest.end() once. | ||
8485 | if (!dest._isStdio && (!options || options.end !== false)) { | ||
8486 | source.on('end', onend); | ||
8487 | source.on('close', onclose); | ||
8488 | } | ||
8489 | |||
8490 | var didOnEnd = false; | ||
8491 | function onend() { | ||
8492 | if (didOnEnd) return; | ||
8493 | didOnEnd = true; | ||
8494 | |||
8495 | dest.end(); | ||
8496 | } | ||
8497 | |||
8498 | |||
8499 | function onclose() { | ||
8500 | if (didOnEnd) return; | ||
8501 | didOnEnd = true; | ||
8502 | |||
8503 | if (typeof dest.destroy === 'function') dest.destroy(); | ||
8504 | } | ||
8505 | |||
8506 | // don't leave dangling pipes when there are errors. | ||
8507 | function onerror(er) { | ||
8508 | cleanup(); | ||
8509 | if (EE.listenerCount(this, 'error') === 0) { | ||
8510 | throw er; // Unhandled stream error in pipe. | ||
8511 | } | ||
8512 | } | ||
8513 | |||
8514 | source.on('error', onerror); | ||
8515 | dest.on('error', onerror); | ||
8516 | |||
8517 | // remove all the event listeners that were added. | ||
8518 | function cleanup() { | ||
8519 | source.removeListener('data', ondata); | ||
8520 | dest.removeListener('drain', ondrain); | ||
8521 | |||
8522 | source.removeListener('end', onend); | ||
8523 | source.removeListener('close', onclose); | ||
8524 | |||
8525 | source.removeListener('error', onerror); | ||
8526 | dest.removeListener('error', onerror); | ||
8527 | |||
8528 | source.removeListener('end', cleanup); | ||
8529 | source.removeListener('close', cleanup); | ||
8530 | |||
8531 | dest.removeListener('close', cleanup); | ||
8532 | } | ||
8533 | |||
8534 | source.on('end', cleanup); | ||
8535 | source.on('close', cleanup); | ||
8536 | |||
8537 | dest.on('close', cleanup); | ||
8538 | |||
8539 | dest.emit('pipe', source); | ||
8540 | |||
8541 | // Allow for unix-like usage: A.pipe(B).pipe(C) | ||
8542 | return dest; | ||
8543 | }; | ||
8544 | |||
8545 | },{"events":18,"inherits":21,"readable-stream/duplex.js":26,"readable-stream/passthrough.js":35,"readable-stream/readable.js":36,"readable-stream/transform.js":37,"readable-stream/writable.js":38}],50:[function(require,module,exports){ | ||
8546 | 'use strict'; | ||
8547 | |||
8548 | var Buffer = require('safe-buffer').Buffer; | ||
8549 | |||
8550 | var isEncoding = Buffer.isEncoding || function (encoding) { | ||
8551 | encoding = '' + encoding; | ||
8552 | switch (encoding && encoding.toLowerCase()) { | ||
8553 | 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': | ||
8554 | return true; | ||
8555 | default: | ||
8556 | return false; | ||
8557 | } | ||
8558 | }; | ||
8559 | |||
8560 | function _normalizeEncoding(enc) { | ||
8561 | if (!enc) return 'utf8'; | ||
8562 | var retried; | ||
8563 | while (true) { | ||
8564 | switch (enc) { | ||
8565 | case 'utf8': | ||
8566 | case 'utf-8': | ||
8567 | return 'utf8'; | ||
8568 | case 'ucs2': | ||
8569 | case 'ucs-2': | ||
8570 | case 'utf16le': | ||
8571 | case 'utf-16le': | ||
8572 | return 'utf16le'; | ||
8573 | case 'latin1': | ||
8574 | case 'binary': | ||
8575 | return 'latin1'; | ||
8576 | case 'base64': | ||
8577 | case 'ascii': | ||
8578 | case 'hex': | ||
8579 | return enc; | ||
8580 | default: | ||
8581 | if (retried) return; // undefined | ||
8582 | enc = ('' + enc).toLowerCase(); | ||
8583 | retried = true; | ||
8584 | } | ||
8585 | } | ||
8586 | }; | ||
8587 | |||
8588 | // Do not cache `Buffer.isEncoding` when checking encoding names as some | ||
8589 | // modules monkey-patch it to support additional encodings | ||
8590 | function normalizeEncoding(enc) { | ||
8591 | var nenc = _normalizeEncoding(enc); | ||
8592 | if (typeof nenc !== 'string' && (Buffer.isEncoding === isEncoding || !isEncoding(enc))) throw new Error('Unknown encoding: ' + enc); | ||
8593 | return nenc || enc; | ||
8594 | } | ||
8595 | |||
8596 | // StringDecoder provides an interface for efficiently splitting a series of | ||
8597 | // buffers into a series of JS strings without breaking apart multi-byte | ||
8598 | // characters. | ||
8599 | exports.StringDecoder = StringDecoder; | ||
8600 | function StringDecoder(encoding) { | ||
8601 | this.encoding = normalizeEncoding(encoding); | ||
8602 | var nb; | ||
8603 | switch (this.encoding) { | ||
8604 | case 'utf16le': | ||
8605 | this.text = utf16Text; | ||
8606 | this.end = utf16End; | ||
8607 | nb = 4; | ||
8608 | break; | ||
8609 | case 'utf8': | ||
8610 | this.fillLast = utf8FillLast; | ||
8611 | nb = 4; | ||
8612 | break; | ||
8613 | case 'base64': | ||
8614 | this.text = base64Text; | ||
8615 | this.end = base64End; | ||
8616 | nb = 3; | ||
8617 | break; | ||
8618 | default: | ||
8619 | this.write = simpleWrite; | ||
8620 | this.end = simpleEnd; | ||
8621 | return; | ||
8622 | } | ||
8623 | this.lastNeed = 0; | ||
8624 | this.lastTotal = 0; | ||
8625 | this.lastChar = Buffer.allocUnsafe(nb); | ||
8626 | } | ||
8627 | |||
8628 | StringDecoder.prototype.write = function (buf) { | ||
8629 | if (buf.length === 0) return ''; | ||
8630 | var r; | ||
8631 | var i; | ||
8632 | if (this.lastNeed) { | ||
8633 | r = this.fillLast(buf); | ||
8634 | if (r === undefined) return ''; | ||
8635 | i = this.lastNeed; | ||
8636 | this.lastNeed = 0; | ||
8637 | } else { | ||
8638 | i = 0; | ||
8639 | } | ||
8640 | if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i); | ||
8641 | return r || ''; | ||
8642 | }; | ||
8643 | |||
8644 | StringDecoder.prototype.end = utf8End; | ||
8645 | |||
8646 | // Returns only complete characters in a Buffer | ||
8647 | StringDecoder.prototype.text = utf8Text; | ||
8648 | |||
8649 | // Attempts to complete a partial non-UTF-8 character using bytes from a Buffer | ||
8650 | StringDecoder.prototype.fillLast = function (buf) { | ||
8651 | if (this.lastNeed <= buf.length) { | ||
8652 | buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed); | ||
8653 | return this.lastChar.toString(this.encoding, 0, this.lastTotal); | ||
8654 | } | ||
8655 | buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length); | ||
8656 | this.lastNeed -= buf.length; | ||
8657 | }; | ||
8658 | |||
8659 | // Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a | ||
8660 | // continuation byte. | ||
8661 | function utf8CheckByte(byte) { | ||
8662 | if (byte <= 0x7F) return 0;else if (byte >> 5 === 0x06) return 2;else if (byte >> 4 === 0x0E) return 3;else if (byte >> 3 === 0x1E) return 4; | ||
8663 | return -1; | ||
8664 | } | ||
8665 | |||
8666 | // Checks at most 3 bytes at the end of a Buffer in order to detect an | ||
8667 | // incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4) | ||
8668 | // needed to complete the UTF-8 character (if applicable) are returned. | ||
8669 | function utf8CheckIncomplete(self, buf, i) { | ||
8670 | var j = buf.length - 1; | ||
8671 | if (j < i) return 0; | ||
8672 | var nb = utf8CheckByte(buf[j]); | ||
8673 | if (nb >= 0) { | ||
8674 | if (nb > 0) self.lastNeed = nb - 1; | ||
8675 | return nb; | ||
8676 | } | ||
8677 | if (--j < i) return 0; | ||
8678 | nb = utf8CheckByte(buf[j]); | ||
8679 | if (nb >= 0) { | ||
8680 | if (nb > 0) self.lastNeed = nb - 2; | ||
8681 | return nb; | ||
8682 | } | ||
8683 | if (--j < i) return 0; | ||
8684 | nb = utf8CheckByte(buf[j]); | ||
8685 | if (nb >= 0) { | ||
8686 | if (nb > 0) { | ||
8687 | if (nb === 2) nb = 0;else self.lastNeed = nb - 3; | ||
8688 | } | ||
8689 | return nb; | ||
8690 | } | ||
8691 | return 0; | ||
8692 | } | ||
8693 | |||
8694 | // Validates as many continuation bytes for a multi-byte UTF-8 character as | ||
8695 | // needed or are available. If we see a non-continuation byte where we expect | ||
8696 | // one, we "replace" the validated continuation bytes we've seen so far with | ||
8697 | // UTF-8 replacement characters ('\ufffd'), to match v8's UTF-8 decoding | ||
8698 | // behavior. The continuation byte check is included three times in the case | ||
8699 | // where all of the continuation bytes for a character exist in the same buffer. | ||
8700 | // It is also done this way as a slight performance increase instead of using a | ||
8701 | // loop. | ||
8702 | function utf8CheckExtraBytes(self, buf, p) { | ||
8703 | if ((buf[0] & 0xC0) !== 0x80) { | ||
8704 | self.lastNeed = 0; | ||
8705 | return '\ufffd'.repeat(p); | ||
8706 | } | ||
8707 | if (self.lastNeed > 1 && buf.length > 1) { | ||
8708 | if ((buf[1] & 0xC0) !== 0x80) { | ||
8709 | self.lastNeed = 1; | ||
8710 | return '\ufffd'.repeat(p + 1); | ||
8711 | } | ||
8712 | if (self.lastNeed > 2 && buf.length > 2) { | ||
8713 | if ((buf[2] & 0xC0) !== 0x80) { | ||
8714 | self.lastNeed = 2; | ||
8715 | return '\ufffd'.repeat(p + 2); | ||
8716 | } | ||
8717 | } | ||
8718 | } | ||
8719 | } | ||
8720 | |||
8721 | // Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer. | ||
8722 | function utf8FillLast(buf) { | ||
8723 | var p = this.lastTotal - this.lastNeed; | ||
8724 | var r = utf8CheckExtraBytes(this, buf, p); | ||
8725 | if (r !== undefined) return r; | ||
8726 | if (this.lastNeed <= buf.length) { | ||
8727 | buf.copy(this.lastChar, p, 0, this.lastNeed); | ||
8728 | return this.lastChar.toString(this.encoding, 0, this.lastTotal); | ||
8729 | } | ||
8730 | buf.copy(this.lastChar, p, 0, buf.length); | ||
8731 | this.lastNeed -= buf.length; | ||
8732 | } | ||
8733 | |||
8734 | // Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a | ||
8735 | // partial character, the character's bytes are buffered until the required | ||
8736 | // number of bytes are available. | ||
8737 | function utf8Text(buf, i) { | ||
8738 | var total = utf8CheckIncomplete(this, buf, i); | ||
8739 | if (!this.lastNeed) return buf.toString('utf8', i); | ||
8740 | this.lastTotal = total; | ||
8741 | var end = buf.length - (total - this.lastNeed); | ||
8742 | buf.copy(this.lastChar, 0, end); | ||
8743 | return buf.toString('utf8', i, end); | ||
8744 | } | ||
8745 | |||
8746 | // For UTF-8, a replacement character for each buffered byte of a (partial) | ||
8747 | // character needs to be added to the output. | ||
8748 | function utf8End(buf) { | ||
8749 | var r = buf && buf.length ? this.write(buf) : ''; | ||
8750 | if (this.lastNeed) return r + '\ufffd'.repeat(this.lastTotal - this.lastNeed); | ||
8751 | return r; | ||
8752 | } | ||
8753 | |||
8754 | // UTF-16LE typically needs two bytes per character, but even if we have an even | ||
8755 | // number of bytes available, we need to check if we end on a leading/high | ||
8756 | // surrogate. In that case, we need to wait for the next two bytes in order to | ||
8757 | // decode the last character properly. | ||
8758 | function utf16Text(buf, i) { | ||
8759 | if ((buf.length - i) % 2 === 0) { | ||
8760 | var r = buf.toString('utf16le', i); | ||
8761 | if (r) { | ||
8762 | var c = r.charCodeAt(r.length - 1); | ||
8763 | if (c >= 0xD800 && c <= 0xDBFF) { | ||
8764 | this.lastNeed = 2; | ||
8765 | this.lastTotal = 4; | ||
8766 | this.lastChar[0] = buf[buf.length - 2]; | ||
8767 | this.lastChar[1] = buf[buf.length - 1]; | ||
8768 | return r.slice(0, -1); | ||
8769 | } | ||
8770 | } | ||
8771 | return r; | ||
8772 | } | ||
8773 | this.lastNeed = 1; | ||
8774 | this.lastTotal = 2; | ||
8775 | this.lastChar[0] = buf[buf.length - 1]; | ||
8776 | return buf.toString('utf16le', i, buf.length - 1); | ||
8777 | } | ||
8778 | |||
8779 | // For UTF-16LE we do not explicitly append special replacement characters if we | ||
8780 | // end on a partial character, we simply let v8 handle that. | ||
8781 | function utf16End(buf) { | ||
8782 | var r = buf && buf.length ? this.write(buf) : ''; | ||
8783 | if (this.lastNeed) { | ||
8784 | var end = this.lastTotal - this.lastNeed; | ||
8785 | return r + this.lastChar.toString('utf16le', 0, end); | ||
8786 | } | ||
8787 | return r; | ||
8788 | } | ||
8789 | |||
8790 | function base64Text(buf, i) { | ||
8791 | var n = (buf.length - i) % 3; | ||
8792 | if (n === 0) return buf.toString('base64', i); | ||
8793 | this.lastNeed = 3 - n; | ||
8794 | this.lastTotal = 3; | ||
8795 | if (n === 1) { | ||
8796 | this.lastChar[0] = buf[buf.length - 1]; | ||
8797 | } else { | ||
8798 | this.lastChar[0] = buf[buf.length - 2]; | ||
8799 | this.lastChar[1] = buf[buf.length - 1]; | ||
8800 | } | ||
8801 | return buf.toString('base64', i, buf.length - n); | ||
8802 | } | ||
8803 | |||
8804 | function base64End(buf) { | ||
8805 | var r = buf && buf.length ? this.write(buf) : ''; | ||
8806 | if (this.lastNeed) return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed); | ||
8807 | return r; | ||
8808 | } | ||
8809 | |||
8810 | // Pass bytes on through for single-byte encodings (e.g. ascii, latin1, hex) | ||
8811 | function simpleWrite(buf) { | ||
8812 | return buf.toString(this.encoding); | ||
8813 | } | ||
8814 | |||
8815 | function simpleEnd(buf) { | ||
8816 | return buf && buf.length ? this.write(buf) : ''; | ||
8817 | } | ||
8818 | },{"safe-buffer":40}],51:[function(require,module,exports){ | ||
8819 | (function (global){ | ||
8820 | |||
8821 | /** | ||
8822 | * Module exports. | ||
8823 | */ | ||
8824 | |||
8825 | module.exports = deprecate; | ||
8826 | |||
8827 | /** | ||
8828 | * Mark that a method should not be used. | ||
8829 | * Returns a modified function which warns once by default. | ||
8830 | * | ||
8831 | * If `localStorage.noDeprecation = true` is set, then it is a no-op. | ||
8832 | * | ||
8833 | * If `localStorage.throwDeprecation = true` is set, then deprecated functions | ||
8834 | * will throw an Error when invoked. | ||
8835 | * | ||
8836 | * If `localStorage.traceDeprecation = true` is set, then deprecated functions | ||
8837 | * will invoke `console.trace()` instead of `console.error()`. | ||
8838 | * | ||
8839 | * @param {Function} fn - the function to deprecate | ||
8840 | * @param {String} msg - the string to print to the console when `fn` is invoked | ||
8841 | * @returns {Function} a new "deprecated" version of `fn` | ||
8842 | * @api public | ||
8843 | */ | ||
8844 | |||
8845 | function deprecate (fn, msg) { | ||
8846 | if (config('noDeprecation')) { | ||
8847 | return fn; | ||
8848 | } | ||
8849 | |||
8850 | var warned = false; | ||
8851 | function deprecated() { | ||
8852 | if (!warned) { | ||
8853 | if (config('throwDeprecation')) { | ||
8854 | throw new Error(msg); | ||
8855 | } else if (config('traceDeprecation')) { | ||
8856 | console.trace(msg); | ||
8857 | } else { | ||
8858 | console.warn(msg); | ||
8859 | } | ||
8860 | warned = true; | ||
8861 | } | ||
8862 | return fn.apply(this, arguments); | ||
8863 | } | ||
8864 | |||
8865 | return deprecated; | ||
8866 | } | ||
8867 | |||
8868 | /** | ||
8869 | * Checks `localStorage` for boolean values for the given `name`. | ||
8870 | * | ||
8871 | * @param {String} name | ||
8872 | * @returns {Boolean} | ||
8873 | * @api private | ||
8874 | */ | ||
8875 | |||
8876 | function config (name) { | ||
8877 | // accessing global.localStorage can trigger a DOMException in sandboxed iframes | ||
8878 | try { | ||
8879 | if (!global.localStorage) return false; | ||
8880 | } catch (_) { | ||
8881 | return false; | ||
8882 | } | ||
8883 | var val = global.localStorage[name]; | ||
8884 | if (null == val) return false; | ||
8885 | return String(val).toLowerCase() === 'true'; | ||
8886 | } | ||
8887 | |||
8888 | }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | ||
8889 | },{}],52:[function(require,module,exports){ | ||
8890 | (function (Buffer){ | ||
8891 | /*** | ||
8892 | * @license | ||
8893 | * https://github.com/bitcoincashjs/bchaddr | ||
8894 | * Copyright (c) 2018 Emilio Almansi | ||
8895 | * Distributed under the MIT software license, see the accompanying | ||
8896 | * file LICENSE or http://www.opensource.org/licenses/mit-license.php. | ||
8897 | */ | ||
8898 | |||
8899 | var bs58check = require('bs58check') | ||
8900 | var cashaddr = require('cashaddrjs') | ||
8901 | |||
8902 | /** | ||
8903 | * General purpose Bitcoin Cash address detection and translation.<br /> | ||
8904 | * Supports all major Bitcoin Cash address formats.<br /> | ||
8905 | * Currently: | ||
8906 | * <ul> | ||
8907 | * <li> Legacy format </li> | ||
8908 | * <li> Bitpay format </li> | ||
8909 | * <li> Cashaddr format </li> | ||
8910 | * </ul> | ||
8911 | * @module bchaddr | ||
8912 | */ | ||
8913 | |||
8914 | /** | ||
8915 | * @static | ||
8916 | * Supported Bitcoin Cash address formats. | ||
8917 | */ | ||
8918 | var Format = {} | ||
8919 | Format.Legacy = 'legacy' | ||
8920 | Format.Bitpay = 'bitpay' | ||
8921 | Format.Cashaddr = 'cashaddr' | ||
8922 | |||
8923 | /** | ||
8924 | * @static | ||
8925 | * Supported networks. | ||
8926 | */ | ||
8927 | var Network = {} | ||
8928 | Network.Mainnet = 'mainnet' | ||
8929 | Network.Testnet = 'testnet' | ||
8930 | |||
8931 | /** | ||
8932 | * @static | ||
8933 | * Supported address types. | ||
8934 | */ | ||
8935 | var Type = {} | ||
8936 | Type.P2PKH = 'p2pkh' | ||
8937 | Type.P2SH = 'p2sh' | ||
8938 | |||
8939 | /** | ||
8940 | * Detects what is the given address' format. | ||
8941 | * @static | ||
8942 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
8943 | * @return {string} | ||
8944 | * @throws {InvalidAddressError} | ||
8945 | */ | ||
8946 | function detectAddressFormat (address) { | ||
8947 | return decodeAddress(address).format | ||
8948 | } | ||
8949 | |||
8950 | /** | ||
8951 | * Detects what is the given address' network. | ||
8952 | * @static | ||
8953 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
8954 | * @return {string} | ||
8955 | * @throws {InvalidAddressError} | ||
8956 | */ | ||
8957 | function detectAddressNetwork (address) { | ||
8958 | return decodeAddress(address).network | ||
8959 | } | ||
8960 | |||
8961 | /** | ||
8962 | * Detects what is the given address' type. | ||
8963 | * @static | ||
8964 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
8965 | * @return {string} | ||
8966 | * @throws {InvalidAddressError} | ||
8967 | */ | ||
8968 | function detectAddressType (address) { | ||
8969 | return decodeAddress(address).type | ||
8970 | } | ||
8971 | |||
8972 | /** | ||
8973 | * Translates the given address into legacy format. | ||
8974 | * @static | ||
8975 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
8976 | * @return {string} | ||
8977 | * @throws {InvalidAddressError} | ||
8978 | */ | ||
8979 | function toLegacyAddress (address) { | ||
8980 | var decoded = decodeAddress(address) | ||
8981 | if (decoded.format === Format.Legacy) { | ||
8982 | return address | ||
8983 | } | ||
8984 | return encodeAsLegacy(decoded) | ||
8985 | } | ||
8986 | |||
8987 | /** | ||
8988 | * Translates the given address into bitpay format. | ||
8989 | * @static | ||
8990 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
8991 | * @return {string} | ||
8992 | * @throws {InvalidAddressError} | ||
8993 | */ | ||
8994 | function toBitpayAddress (address) { | ||
8995 | var decoded = decodeAddress(address) | ||
8996 | if (decoded.format === Format.Bitpay) { | ||
8997 | return address | ||
8998 | } | ||
8999 | return encodeAsBitpay(decoded) | ||
9000 | } | ||
9001 | |||
9002 | /** | ||
9003 | * Translates the given address into cashaddr format. | ||
9004 | * @static | ||
9005 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9006 | * @return {string} | ||
9007 | * @throws {InvalidAddressError} | ||
9008 | */ | ||
9009 | function toCashAddress (address) { | ||
9010 | var decoded = decodeAddress(address) | ||
9011 | return encodeAsCashaddr(decoded) | ||
9012 | } | ||
9013 | |||
9014 | /** | ||
9015 | * Version byte table for base58 formats. | ||
9016 | * @private | ||
9017 | */ | ||
9018 | var VERSION_BYTE = {} | ||
9019 | VERSION_BYTE[Format.Legacy] = {} | ||
9020 | VERSION_BYTE[Format.Legacy][Network.Mainnet] = {} | ||
9021 | VERSION_BYTE[Format.Legacy][Network.Mainnet][Type.P2PKH] = 0 | ||
9022 | VERSION_BYTE[Format.Legacy][Network.Mainnet][Type.P2SH] = 5 | ||
9023 | VERSION_BYTE[Format.Legacy][Network.Testnet] = {} | ||
9024 | VERSION_BYTE[Format.Legacy][Network.Testnet][Type.P2PKH] = 111 | ||
9025 | VERSION_BYTE[Format.Legacy][Network.Testnet][Type.P2SH] = 196 | ||
9026 | VERSION_BYTE[Format.Bitpay] = {} | ||
9027 | VERSION_BYTE[Format.Bitpay][Network.Mainnet] = {} | ||
9028 | VERSION_BYTE[Format.Bitpay][Network.Mainnet][Type.P2PKH] = 28 | ||
9029 | VERSION_BYTE[Format.Bitpay][Network.Mainnet][Type.P2SH] = 40 | ||
9030 | VERSION_BYTE[Format.Bitpay][Network.Testnet] = {} | ||
9031 | VERSION_BYTE[Format.Bitpay][Network.Testnet][Type.P2PKH] = 111 | ||
9032 | VERSION_BYTE[Format.Bitpay][Network.Testnet][Type.P2SH] = 196 | ||
9033 | |||
9034 | /** | ||
9035 | * Decodes the given address into its constituting hash, format, network and type. | ||
9036 | * @private | ||
9037 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9038 | * @return {object} | ||
9039 | * @throws {InvalidAddressError} | ||
9040 | */ | ||
9041 | function decodeAddress (address) { | ||
9042 | try { | ||
9043 | return decodeBase58Address(address) | ||
9044 | } catch (error) { | ||
9045 | } | ||
9046 | try { | ||
9047 | return decodeCashAddress(address) | ||
9048 | } catch (error) { | ||
9049 | } | ||
9050 | throw new InvalidAddressError() | ||
9051 | } | ||
9052 | |||
9053 | /** | ||
9054 | * Attempts to decode the given address assuming it is a base58 address. | ||
9055 | * @private | ||
9056 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9057 | * @return {object} | ||
9058 | * @throws {InvalidAddressError} | ||
9059 | */ | ||
9060 | function decodeBase58Address (address) { | ||
9061 | try { | ||
9062 | var payload = bs58check.decode(address) | ||
9063 | var versionByte = payload[0] | ||
9064 | var hash = Array.prototype.slice.call(payload, 1) | ||
9065 | switch (versionByte) { | ||
9066 | case VERSION_BYTE[Format.Legacy][Network.Mainnet][Type.P2PKH]: | ||
9067 | return { | ||
9068 | hash: hash, | ||
9069 | format: Format.Legacy, | ||
9070 | network: Network.Mainnet, | ||
9071 | type: Type.P2PKH | ||
9072 | } | ||
9073 | case VERSION_BYTE[Format.Legacy][Network.Mainnet][Type.P2SH]: | ||
9074 | return { | ||
9075 | hash: hash, | ||
9076 | format: Format.Legacy, | ||
9077 | network: Network.Mainnet, | ||
9078 | type: Type.P2SH | ||
9079 | } | ||
9080 | case VERSION_BYTE[Format.Legacy][Network.Testnet][Type.P2PKH]: | ||
9081 | return { | ||
9082 | hash: hash, | ||
9083 | format: Format.Legacy, | ||
9084 | network: Network.Testnet, | ||
9085 | type: Type.P2PKH | ||
9086 | } | ||
9087 | case VERSION_BYTE[Format.Legacy][Network.Testnet][Type.P2SH]: | ||
9088 | return { | ||
9089 | hash: hash, | ||
9090 | format: Format.Legacy, | ||
9091 | network: Network.Testnet, | ||
9092 | type: Type.P2SH | ||
9093 | } | ||
9094 | case VERSION_BYTE[Format.Bitpay][Network.Mainnet][Type.P2PKH]: | ||
9095 | return { | ||
9096 | hash: hash, | ||
9097 | format: Format.Bitpay, | ||
9098 | network: Network.Mainnet, | ||
9099 | type: Type.P2PKH | ||
9100 | } | ||
9101 | case VERSION_BYTE[Format.Bitpay][Network.Mainnet][Type.P2SH]: | ||
9102 | return { | ||
9103 | hash: hash, | ||
9104 | format: Format.Bitpay, | ||
9105 | network: Network.Mainnet, | ||
9106 | type: Type.P2SH | ||
9107 | } | ||
9108 | } | ||
9109 | } catch (error) { | ||
9110 | } | ||
9111 | throw new InvalidAddressError() | ||
9112 | } | ||
9113 | |||
9114 | /** | ||
9115 | * Attempts to decode the given address assuming it is a cashaddr address. | ||
9116 | * @private | ||
9117 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9118 | * @return {object} | ||
9119 | * @throws {InvalidAddressError} | ||
9120 | */ | ||
9121 | function decodeCashAddress (address) { | ||
9122 | if (address.indexOf(':') !== -1) { | ||
9123 | try { | ||
9124 | return decodeCashAddressWithPrefix(address) | ||
9125 | } catch (error) { | ||
9126 | } | ||
9127 | } else { | ||
9128 | var prefixes = ['bitcoincash', 'bchtest', 'regtest'] | ||
9129 | for (var i = 0; i < prefixes.length; ++i) { | ||
9130 | try { | ||
9131 | var prefix = prefixes[i] | ||
9132 | return decodeCashAddressWithPrefix(prefix + ':' + address) | ||
9133 | } catch (error) { | ||
9134 | } | ||
9135 | } | ||
9136 | } | ||
9137 | throw new InvalidAddressError() | ||
9138 | } | ||
9139 | |||
9140 | /** | ||
9141 | * Attempts to decode the given address assuming it is a cashaddr address with explicit prefix. | ||
9142 | * @private | ||
9143 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9144 | * @return {object} | ||
9145 | * @throws {InvalidAddressError} | ||
9146 | */ | ||
9147 | function decodeCashAddressWithPrefix (address) { | ||
9148 | try { | ||
9149 | var decoded = cashaddr.decode(address) | ||
9150 | var hash = Array.prototype.slice.call(decoded.hash, 0) | ||
9151 | var type = decoded.type === 'P2PKH' ? Type.P2PKH : Type.P2SH | ||
9152 | switch (decoded.prefix) { | ||
9153 | case 'bitcoincash': | ||
9154 | return { | ||
9155 | hash: hash, | ||
9156 | format: Format.Cashaddr, | ||
9157 | network: Network.Mainnet, | ||
9158 | type: type | ||
9159 | } | ||
9160 | case 'bchtest': | ||
9161 | case 'regtest': | ||
9162 | return { | ||
9163 | hash: hash, | ||
9164 | format: Format.Cashaddr, | ||
9165 | network: Network.Testnet, | ||
9166 | type: type | ||
9167 | } | ||
9168 | } | ||
9169 | } catch (error) { | ||
9170 | } | ||
9171 | throw new InvalidAddressError() | ||
9172 | } | ||
9173 | |||
9174 | /** | ||
9175 | * Encodes the given decoded address into legacy format. | ||
9176 | * @private | ||
9177 | * @param {object} decoded | ||
9178 | * @returns {string} | ||
9179 | */ | ||
9180 | function encodeAsLegacy (decoded) { | ||
9181 | var versionByte = VERSION_BYTE[Format.Legacy][decoded.network][decoded.type] | ||
9182 | var buffer = Buffer.alloc(1 + decoded.hash.length) | ||
9183 | buffer[0] = versionByte | ||
9184 | buffer.set(decoded.hash, 1) | ||
9185 | return bs58check.encode(buffer) | ||
9186 | } | ||
9187 | |||
9188 | /** | ||
9189 | * Encodes the given decoded address into bitpay format. | ||
9190 | * @private | ||
9191 | * @param {object} decoded | ||
9192 | * @returns {string} | ||
9193 | */ | ||
9194 | function encodeAsBitpay (decoded) { | ||
9195 | var versionByte = VERSION_BYTE[Format.Bitpay][decoded.network][decoded.type] | ||
9196 | var buffer = Buffer.alloc(1 + decoded.hash.length) | ||
9197 | buffer[0] = versionByte | ||
9198 | buffer.set(decoded.hash, 1) | ||
9199 | return bs58check.encode(buffer) | ||
9200 | } | ||
9201 | |||
9202 | /** | ||
9203 | * Encodes the given decoded address into cashaddr format. | ||
9204 | * @private | ||
9205 | * @param {object} decoded | ||
9206 | * @returns {string} | ||
9207 | */ | ||
9208 | function encodeAsCashaddr (decoded) { | ||
9209 | var prefix = decoded.network === Network.Mainnet ? 'bitcoincash' : 'bchtest' | ||
9210 | var type = decoded.type === Type.P2PKH ? 'P2PKH' : 'P2SH' | ||
9211 | var hash = Uint8Array.from(decoded.hash) | ||
9212 | return cashaddr.encode(prefix, type, hash) | ||
9213 | } | ||
9214 | |||
9215 | /** | ||
9216 | * Returns a boolean indicating whether the address is in legacy format. | ||
9217 | * @static | ||
9218 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9219 | * @returns {boolean} | ||
9220 | * @throws {InvalidAddressError} | ||
9221 | */ | ||
9222 | function isLegacyAddress (address) { | ||
9223 | return detectAddressFormat(address) === Format.Legacy | ||
9224 | } | ||
9225 | |||
9226 | /** | ||
9227 | * Returns a boolean indicating whether the address is in bitpay format. | ||
9228 | * @static | ||
9229 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9230 | * @returns {boolean} | ||
9231 | * @throws {InvalidAddressError} | ||
9232 | */ | ||
9233 | function isBitpayAddress (address) { | ||
9234 | return detectAddressFormat(address) === Format.Bitpay | ||
9235 | } | ||
9236 | |||
9237 | /** | ||
9238 | * Returns a boolean indicating whether the address is in cashaddr format. | ||
9239 | * @static | ||
9240 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9241 | * @returns {boolean} | ||
9242 | * @throws {InvalidAddressError} | ||
9243 | */ | ||
9244 | function isCashAddress (address) { | ||
9245 | return detectAddressFormat(address) === Format.Cashaddr | ||
9246 | } | ||
9247 | |||
9248 | /** | ||
9249 | * Returns a boolean indicating whether the address is a mainnet address. | ||
9250 | * @static | ||
9251 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9252 | * @returns {boolean} | ||
9253 | * @throws {InvalidAddressError} | ||
9254 | */ | ||
9255 | function isMainnetAddress (address) { | ||
9256 | return detectAddressNetwork(address) === Network.Mainnet | ||
9257 | } | ||
9258 | |||
9259 | /** | ||
9260 | * Returns a boolean indicating whether the address is a testnet address. | ||
9261 | * @static | ||
9262 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9263 | * @returns {boolean} | ||
9264 | * @throws {InvalidAddressError} | ||
9265 | */ | ||
9266 | function isTestnetAddress (address) { | ||
9267 | return detectAddressNetwork(address) === Network.Testnet | ||
9268 | } | ||
9269 | |||
9270 | /** | ||
9271 | * Returns a boolean indicating whether the address is a p2pkh address. | ||
9272 | * @static | ||
9273 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9274 | * @returns {boolean} | ||
9275 | * @throws {InvalidAddressError} | ||
9276 | */ | ||
9277 | function isP2PKHAddress (address) { | ||
9278 | return detectAddressType(address) === Type.P2PKH | ||
9279 | } | ||
9280 | |||
9281 | /** | ||
9282 | * Returns a boolean indicating whether the address is a p2sh address. | ||
9283 | * @static | ||
9284 | * @param {string} address - A valid Bitcoin Cash address in any format. | ||
9285 | * @returns {boolean} | ||
9286 | * @throws {InvalidAddressError} | ||
9287 | */ | ||
9288 | function isP2SHAddress (address) { | ||
9289 | return detectAddressType(address) === Type.P2SH | ||
9290 | } | ||
9291 | |||
9292 | /** | ||
9293 | * Error thrown when the address given as input is not a valid Bitcoin Cash address. | ||
9294 | * @constructor | ||
9295 | * InvalidAddressError | ||
9296 | */ | ||
9297 | function InvalidAddressError () { | ||
9298 | var error = new Error() | ||
9299 | this.name = error.name = 'InvalidAddressError' | ||
9300 | this.message = error.message = 'Received an invalid Bitcoin Cash address as input.' | ||
9301 | this.stack = error.stack | ||
9302 | } | ||
9303 | |||
9304 | InvalidAddressError.prototype = Object.create(Error.prototype) | ||
9305 | |||
9306 | module.exports = { | ||
9307 | Format: Format, | ||
9308 | Network: Network, | ||
9309 | Type: Type, | ||
9310 | detectAddressFormat: detectAddressFormat, | ||
9311 | detectAddressNetwork: detectAddressNetwork, | ||
9312 | detectAddressType: detectAddressType, | ||
9313 | toLegacyAddress: toLegacyAddress, | ||
9314 | toBitpayAddress: toBitpayAddress, | ||
9315 | toCashAddress: toCashAddress, | ||
9316 | isLegacyAddress: isLegacyAddress, | ||
9317 | isBitpayAddress: isBitpayAddress, | ||
9318 | isCashAddress: isCashAddress, | ||
9319 | isMainnetAddress: isMainnetAddress, | ||
9320 | isTestnetAddress: isTestnetAddress, | ||
9321 | isP2PKHAddress: isP2PKHAddress, | ||
9322 | isP2SHAddress: isP2SHAddress, | ||
9323 | InvalidAddressError: InvalidAddressError | ||
9324 | } | ||
9325 | |||
9326 | }).call(this,require("buffer").Buffer) | ||
9327 | },{"bs58check":7,"buffer":8,"cashaddrjs":10}]},{},[52])(52) | ||
9328 | }); \ No newline at end of file | ||
diff --git a/src/js/bitcoinjs-extensions.js b/src/js/bitcoinjs-extensions.js index 8c0f79e..8fc2d14 100644 --- a/src/js/bitcoinjs-extensions.js +++ b/src/js/bitcoinjs-extensions.js | |||
@@ -307,17 +307,6 @@ bitcoinjs.bitcoin.networks.bgold = { | |||
307 | wif: 128 | 307 | wif: 128 |
308 | }; | 308 | }; |
309 | 309 | ||
310 | bitcoinjs.bitcoin.networks.bitcoinCashBitbpay = { | ||
311 | messagePrefix: '\x18Bitcoin Signed Message:\n', | ||
312 | bip32: { | ||
313 | public: 0x0488b21e, | ||
314 | private: 0x0488ade4 | ||
315 | }, | ||
316 | pubKeyHash: 0x1c, | ||
317 | scriptHash: 0x28, | ||
318 | wif: 0x80 | ||
319 | }; | ||
320 | |||
321 | bitcoinjs.bitcoin.networks.monacoin = { | 310 | bitcoinjs.bitcoin.networks.monacoin = { |
322 | messagePrefix: '\x18Monacoin Signed Message:\n', | 311 | messagePrefix: '\x18Monacoin Signed Message:\n', |
323 | bip32: { | 312 | bip32: { |
diff --git a/src/js/index.js b/src/js/index.js index 1e8b616..375f7f9 100644 --- a/src/js/index.js +++ b/src/js/index.js | |||
@@ -91,8 +91,8 @@ | |||
91 | DOM.generatedStrength = $(".generate-container .strength"); | 91 | DOM.generatedStrength = $(".generate-container .strength"); |
92 | DOM.generatedStrengthWarning = $(".generate-container .warning"); | 92 | DOM.generatedStrengthWarning = $(".generate-container .warning"); |
93 | DOM.hardenedAddresses = $(".hardened-addresses"); | 93 | DOM.hardenedAddresses = $(".hardened-addresses"); |
94 | DOM.useBitpayAddressesContainer = $(".use-bitpay-addresses-container"); | 94 | DOM.bitcoinCashAddressTypeContainer = $(".bch-addr-type-container"); |
95 | DOM.useBitpayAddresses = $(".use-bitpay-addresses"); | 95 | DOM.bitcoinCashAddressType = $("[name=bch-addr-type]") |
96 | DOM.useBip38 = $(".use-bip38"); | 96 | DOM.useBip38 = $(".use-bip38"); |
97 | DOM.bip38Password = $(".bip38-password"); | 97 | DOM.bip38Password = $(".bip38-password"); |
98 | DOM.addresses = $(".addresses"); | 98 | DOM.addresses = $(".addresses"); |
@@ -147,7 +147,7 @@ | |||
147 | DOM.privateKeyToggle.on("click", togglePrivateKeys); | 147 | DOM.privateKeyToggle.on("click", togglePrivateKeys); |
148 | DOM.csvTab.on("click", updateCsv); | 148 | DOM.csvTab.on("click", updateCsv); |
149 | DOM.languages.on("click", languageChanged); | 149 | DOM.languages.on("click", languageChanged); |
150 | DOM.useBitpayAddresses.on("change", useBitpayAddressesChange); | 150 | DOM.bitcoinCashAddressType.on("change", bitcoinCashAddressTypeChange); |
151 | setQrEvents(DOM.showQrEls); | 151 | setQrEvents(DOM.showQrEls); |
152 | disableForms(); | 152 | disableForms(); |
153 | hidePending(); | 153 | hidePending(); |
@@ -172,7 +172,7 @@ | |||
172 | clearDerivedKeys(); | 172 | clearDerivedKeys(); |
173 | clearAddressesList(); | 173 | clearAddressesList(); |
174 | DOM.litecoinLtubContainer.addClass("hidden"); | 174 | DOM.litecoinLtubContainer.addClass("hidden"); |
175 | DOM.useBitpayAddressesContainer.addClass("hidden"); | 175 | DOM.bitcoinCashAddressTypeContainer.addClass("hidden"); |
176 | var networkIndex = e.target.value; | 176 | var networkIndex = e.target.value; |
177 | var network = networks[networkIndex]; | 177 | var network = networks[networkIndex]; |
178 | network.onSelect(); | 178 | network.onSelect(); |
@@ -424,8 +424,7 @@ | |||
424 | }, 50); | 424 | }, 50); |
425 | } | 425 | } |
426 | 426 | ||
427 | function useBitpayAddressesChange() { | 427 | function bitcoinCashAddressTypeChange() { |
428 | setBitcoinCashNetworkValues(); | ||
429 | phraseChanged(); | 428 | phraseChanged(); |
430 | } | 429 | } |
431 | 430 | ||
@@ -852,6 +851,16 @@ | |||
852 | privkey = convertRipplePriv(privkey); | 851 | privkey = convertRipplePriv(privkey); |
853 | address = convertRippleAdrr(address); | 852 | address = convertRippleAdrr(address); |
854 | } | 853 | } |
854 | // Bitcoin Cash address format may vary | ||
855 | if (networks[DOM.network.val()].name == "BCH - Bitcoin Cash") { | ||
856 | var bchAddrType = DOM.bitcoinCashAddressType.filter(":checked").val(); | ||
857 | if (bchAddrType == "cashaddr") { | ||
858 | address = bchaddr.toCashAddress(address); | ||
859 | } | ||
860 | else if (bchAddrType == "bitpay") { | ||
861 | address = bchaddr.toBitpayAddress(address); | ||
862 | } | ||
863 | } | ||
855 | // Segwit addresses are different | 864 | // Segwit addresses are different |
856 | if (isSegwit) { | 865 | if (isSegwit) { |
857 | if (!segwitAvailable) { | 866 | if (!segwitAvailable) { |
@@ -1417,19 +1426,6 @@ | |||
1417 | DOM.bip141unavailable.removeClass("hidden"); | 1426 | DOM.bip141unavailable.removeClass("hidden"); |
1418 | } | 1427 | } |
1419 | 1428 | ||
1420 | function useBitpayAddresses() { | ||
1421 | return !(DOM.useBitpayAddresses.prop("checked")); | ||
1422 | } | ||
1423 | |||
1424 | function setBitcoinCashNetworkValues() { | ||
1425 | if (useBitpayAddresses()) { | ||
1426 | network = bitcoinjs.bitcoin.networks.bitcoin; | ||
1427 | } | ||
1428 | else { | ||
1429 | network = bitcoinjs.bitcoin.networks.bitcoinCashBitbpay; | ||
1430 | } | ||
1431 | } | ||
1432 | |||
1433 | function adjustNetworkForSegwit() { | 1429 | function adjustNetworkForSegwit() { |
1434 | // If segwit is selected the xpub/xprv prefixes need to be adjusted | 1430 | // If segwit is selected the xpub/xprv prefixes need to be adjusted |
1435 | // to avoid accidentally importing BIP49 xpub to BIP44 watch only | 1431 | // to avoid accidentally importing BIP49 xpub to BIP44 watch only |
@@ -1582,8 +1578,7 @@ | |||
1582 | name: "BCH - Bitcoin Cash", | 1578 | name: "BCH - Bitcoin Cash", |
1583 | segwitAvailable: false, | 1579 | segwitAvailable: false, |
1584 | onSelect: function() { | 1580 | onSelect: function() { |
1585 | DOM.useBitpayAddressesContainer.removeClass("hidden"); | 1581 | DOM.bitcoinCashAddressTypeContainer.removeClass("hidden"); |
1586 | setBitcoinCashNetworkValues(); | ||
1587 | setHdCoin(145); | 1582 | setHdCoin(145); |
1588 | }, | 1583 | }, |
1589 | }, | 1584 | }, |