pixel-react 1.15.21 → 1.15.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/lib/_virtual/aesCipherSuites.js +4 -0
  2. package/lib/_virtual/aesCipherSuites.js.map +1 -0
  3. package/lib/_virtual/asn1-validator.js +4 -0
  4. package/lib/_virtual/asn1-validator.js.map +1 -0
  5. package/lib/_virtual/asn1.js +4 -0
  6. package/lib/_virtual/asn1.js.map +1 -0
  7. package/lib/_virtual/cipherModes.js +4 -0
  8. package/lib/_virtual/cipherModes.js.map +1 -0
  9. package/lib/_virtual/hmac.js +4 -0
  10. package/lib/_virtual/hmac.js.map +1 -0
  11. package/lib/_virtual/index11.js +2 -2
  12. package/lib/_virtual/index12.js +2 -2
  13. package/lib/_virtual/index13.js +2 -2
  14. package/lib/_virtual/index14.js +4 -0
  15. package/lib/_virtual/index14.js.map +1 -0
  16. package/lib/_virtual/index2.js +5 -3
  17. package/lib/_virtual/index2.js.map +1 -1
  18. package/lib/_virtual/index3.js +4 -2
  19. package/lib/_virtual/index3.js.map +1 -1
  20. package/lib/_virtual/index4.js +2 -4
  21. package/lib/_virtual/index4.js.map +1 -1
  22. package/lib/_virtual/index5.js +4 -2
  23. package/lib/_virtual/index5.js.map +1 -1
  24. package/lib/_virtual/index6.js +2 -2
  25. package/lib/_virtual/index7.js +2 -6
  26. package/lib/_virtual/index7.js.map +1 -1
  27. package/lib/_virtual/index8.js +6 -2
  28. package/lib/_virtual/index8.js.map +1 -1
  29. package/lib/_virtual/index9.js +2 -2
  30. package/lib/_virtual/md5.js +4 -0
  31. package/lib/_virtual/md5.js.map +1 -0
  32. package/lib/_virtual/mgf1.js +4 -0
  33. package/lib/_virtual/mgf1.js.map +1 -0
  34. package/lib/_virtual/oids.js +4 -0
  35. package/lib/_virtual/oids.js.map +1 -0
  36. package/lib/_virtual/pem.js +4 -0
  37. package/lib/_virtual/pem.js.map +1 -0
  38. package/lib/_virtual/pkcs1.js +4 -0
  39. package/lib/_virtual/pkcs1.js.map +1 -0
  40. package/lib/_virtual/pkcs12.js +4 -0
  41. package/lib/_virtual/pkcs12.js.map +1 -0
  42. package/lib/_virtual/pkcs7.js +4 -0
  43. package/lib/_virtual/pkcs7.js.map +1 -0
  44. package/lib/_virtual/pkcs7asn1.js +4 -0
  45. package/lib/_virtual/pkcs7asn1.js.map +1 -0
  46. package/lib/_virtual/pki.js +4 -0
  47. package/lib/_virtual/pki.js.map +1 -0
  48. package/lib/_virtual/prime.js +4 -0
  49. package/lib/_virtual/prime.js.map +1 -0
  50. package/lib/_virtual/prng.js +4 -0
  51. package/lib/_virtual/prng.js.map +1 -0
  52. package/lib/_virtual/pss.js +4 -0
  53. package/lib/_virtual/pss.js.map +1 -0
  54. package/lib/_virtual/random.js +4 -0
  55. package/lib/_virtual/random.js.map +1 -0
  56. package/lib/_virtual/sha1.js +4 -0
  57. package/lib/_virtual/sha1.js.map +1 -0
  58. package/lib/_virtual/sha256.js +4 -0
  59. package/lib/_virtual/sha256.js.map +1 -0
  60. package/lib/_virtual/sha512.js +4 -0
  61. package/lib/_virtual/sha512.js.map +1 -0
  62. package/lib/_virtual/ssh.js +4 -0
  63. package/lib/_virtual/ssh.js.map +1 -0
  64. package/lib/_virtual/util.js +4 -0
  65. package/lib/_virtual/util.js.map +1 -0
  66. package/lib/_virtual/x509.js +4 -0
  67. package/lib/_virtual/x509.js.map +1 -0
  68. package/lib/assets/icons/testify_icon.svg.js +6 -0
  69. package/lib/assets/icons/testify_icon.svg.js.map +1 -0
  70. package/lib/assets/icons/testify_loader.svg.js +6 -0
  71. package/lib/assets/icons/testify_loader.svg.js.map +1 -0
  72. package/lib/assets/icons/testify_logo_name.svg.js +6 -0
  73. package/lib/assets/icons/testify_logo_name.svg.js.map +1 -0
  74. package/lib/components/AllProjectsDropdown/AllProjectsDropdown.js +3 -1
  75. package/lib/components/AllProjectsDropdown/AllProjectsDropdown.js.map +1 -1
  76. package/lib/components/Charts/DashboardDonutChart/DashboardDonutChart.js +2 -9
  77. package/lib/components/Charts/DashboardDonutChart/DashboardDonutChart.js.map +1 -1
  78. package/lib/components/Charts/DashboardDonutChart/types.d.ts +1 -1
  79. package/lib/components/ConditionalDropdown/ConditionalDropdown.js +2 -9
  80. package/lib/components/ConditionalDropdown/ConditionalDropdown.js.map +1 -1
  81. package/lib/components/Editor/Editor.js +37 -17
  82. package/lib/components/Editor/Editor.js.map +1 -1
  83. package/lib/components/Icon/iconList.js +6 -0
  84. package/lib/components/Icon/iconList.js.map +1 -1
  85. package/lib/components/MultiSelect/MultiSelect.js +9 -10
  86. package/lib/components/MultiSelect/MultiSelect.js.map +1 -1
  87. package/lib/components/PhoneInput/PhoneInput.js +1 -0
  88. package/lib/components/PhoneInput/PhoneInput.js.map +1 -1
  89. package/lib/components/SessionDropdown/SessionDropdown.js +4 -2
  90. package/lib/components/SessionDropdown/SessionDropdown.js.map +1 -1
  91. package/lib/index.cjs +27423 -379
  92. package/lib/index.cjs.map +1 -1
  93. package/lib/index.d.ts +1 -1
  94. package/lib/node_modules/classnames/index.js +1 -1
  95. package/lib/node_modules/input-format/modules/react/Input.js +1 -1
  96. package/lib/node_modules/js-beautify/js/index.js +1 -1
  97. package/lib/node_modules/js-beautify/js/src/html/beautifier.js +1 -1
  98. package/lib/node_modules/js-beautify/js/src/html/index.js +1 -1
  99. package/lib/node_modules/js-beautify/js/src/html/options.js +1 -1
  100. package/lib/node_modules/js-beautify/js/src/html/tokenizer.js +1 -1
  101. package/lib/node_modules/js-beautify/js/src/index.js +1 -1
  102. package/lib/node_modules/js-beautify/js/src/javascript/beautifier.js +1 -1
  103. package/lib/node_modules/js-beautify/js/src/javascript/index.js +1 -1
  104. package/lib/node_modules/js-beautify/js/src/javascript/options.js +1 -1
  105. package/lib/node_modules/js-beautify/js/src/javascript/tokenizer.js +1 -1
  106. package/lib/node_modules/node-forge/lib/aes.js +1014 -0
  107. package/lib/node_modules/node-forge/lib/aes.js.map +1 -0
  108. package/lib/node_modules/node-forge/lib/aesCipherSuites.js +286 -0
  109. package/lib/node_modules/node-forge/lib/aesCipherSuites.js.map +1 -0
  110. package/lib/node_modules/node-forge/lib/asn1-validator.js +100 -0
  111. package/lib/node_modules/node-forge/lib/asn1-validator.js.map +1 -0
  112. package/lib/node_modules/node-forge/lib/asn1.js +1379 -0
  113. package/lib/node_modules/node-forge/lib/asn1.js.map +1 -0
  114. package/lib/node_modules/node-forge/lib/baseN.js +181 -0
  115. package/lib/node_modules/node-forge/lib/baseN.js.map +1 -0
  116. package/lib/node_modules/node-forge/lib/cipher.js +236 -0
  117. package/lib/node_modules/node-forge/lib/cipher.js.map +1 -0
  118. package/lib/node_modules/node-forge/lib/cipherModes.js +936 -0
  119. package/lib/node_modules/node-forge/lib/cipherModes.js.map +1 -0
  120. package/lib/node_modules/node-forge/lib/des.js +467 -0
  121. package/lib/node_modules/node-forge/lib/des.js.map +1 -0
  122. package/lib/node_modules/node-forge/lib/ed25519.js +1108 -0
  123. package/lib/node_modules/node-forge/lib/ed25519.js.map +1 -0
  124. package/lib/node_modules/node-forge/lib/forge.js +23 -0
  125. package/lib/node_modules/node-forge/lib/forge.js.map +1 -0
  126. package/lib/node_modules/node-forge/lib/hmac.js +158 -0
  127. package/lib/node_modules/node-forge/lib/hmac.js.map +1 -0
  128. package/lib/node_modules/node-forge/lib/index.js +70 -0
  129. package/lib/node_modules/node-forge/lib/index.js.map +1 -0
  130. package/lib/node_modules/node-forge/lib/jsbn.js +1481 -0
  131. package/lib/node_modules/node-forge/lib/jsbn.js.map +1 -0
  132. package/lib/node_modules/node-forge/lib/kem.js +178 -0
  133. package/lib/node_modules/node-forge/lib/kem.js.map +1 -0
  134. package/lib/node_modules/node-forge/lib/log.js +325 -0
  135. package/lib/node_modules/node-forge/lib/log.js.map +1 -0
  136. package/lib/node_modules/node-forge/lib/md.all.js +28 -0
  137. package/lib/node_modules/node-forge/lib/md.all.js.map +1 -0
  138. package/lib/node_modules/node-forge/lib/md.js +22 -0
  139. package/lib/node_modules/node-forge/lib/md.js.map +1 -0
  140. package/lib/node_modules/node-forge/lib/md5.js +288 -0
  141. package/lib/node_modules/node-forge/lib/md5.js.map +1 -0
  142. package/lib/node_modules/node-forge/lib/mgf.js +24 -0
  143. package/lib/node_modules/node-forge/lib/mgf.js.map +1 -0
  144. package/lib/node_modules/node-forge/lib/mgf1.js +68 -0
  145. package/lib/node_modules/node-forge/lib/mgf1.js.map +1 -0
  146. package/lib/node_modules/node-forge/lib/oids.js +185 -0
  147. package/lib/node_modules/node-forge/lib/oids.js.map +1 -0
  148. package/lib/node_modules/node-forge/lib/pbe.js +966 -0
  149. package/lib/node_modules/node-forge/lib/pbe.js.map +1 -0
  150. package/lib/node_modules/node-forge/lib/pbkdf2.js +209 -0
  151. package/lib/node_modules/node-forge/lib/pbkdf2.js.map +1 -0
  152. package/lib/node_modules/node-forge/lib/pem.js +250 -0
  153. package/lib/node_modules/node-forge/lib/pem.js.map +1 -0
  154. package/lib/node_modules/node-forge/lib/pkcs1.js +273 -0
  155. package/lib/node_modules/node-forge/lib/pkcs1.js.map +1 -0
  156. package/lib/node_modules/node-forge/lib/pkcs12.js +980 -0
  157. package/lib/node_modules/node-forge/lib/pkcs12.js.map +1 -0
  158. package/lib/node_modules/node-forge/lib/pkcs7.js +1073 -0
  159. package/lib/node_modules/node-forge/lib/pkcs7.js.map +1 -0
  160. package/lib/node_modules/node-forge/lib/pkcs7asn1.js +415 -0
  161. package/lib/node_modules/node-forge/lib/pkcs7asn1.js.map +1 -0
  162. package/lib/node_modules/node-forge/lib/pki.js +125 -0
  163. package/lib/node_modules/node-forge/lib/pki.js.map +1 -0
  164. package/lib/node_modules/node-forge/lib/prime.js +297 -0
  165. package/lib/node_modules/node-forge/lib/prime.js.map +1 -0
  166. package/lib/node_modules/node-forge/lib/prng.js +433 -0
  167. package/lib/node_modules/node-forge/lib/prng.js.map +1 -0
  168. package/lib/node_modules/node-forge/lib/pss.js +246 -0
  169. package/lib/node_modules/node-forge/lib/pss.js.map +1 -0
  170. package/lib/node_modules/node-forge/lib/random.js +191 -0
  171. package/lib/node_modules/node-forge/lib/random.js.map +1 -0
  172. package/lib/node_modules/node-forge/lib/rc2.js +382 -0
  173. package/lib/node_modules/node-forge/lib/rc2.js.map +1 -0
  174. package/lib/node_modules/node-forge/lib/rsa.js +1815 -0
  175. package/lib/node_modules/node-forge/lib/rsa.js.map +1 -0
  176. package/lib/node_modules/node-forge/lib/sha1.js +325 -0
  177. package/lib/node_modules/node-forge/lib/sha1.js.map +1 -0
  178. package/lib/node_modules/node-forge/lib/sha256.js +306 -0
  179. package/lib/node_modules/node-forge/lib/sha256.js.map +1 -0
  180. package/lib/node_modules/node-forge/lib/sha512.js +479 -0
  181. package/lib/node_modules/node-forge/lib/sha512.js.map +1 -0
  182. package/lib/node_modules/node-forge/lib/ssh.js +244 -0
  183. package/lib/node_modules/node-forge/lib/ssh.js.map +1 -0
  184. package/lib/node_modules/node-forge/lib/tls.js +4207 -0
  185. package/lib/node_modules/node-forge/lib/tls.js.map +1 -0
  186. package/lib/node_modules/node-forge/lib/util.js +2565 -0
  187. package/lib/node_modules/node-forge/lib/util.js.map +1 -0
  188. package/lib/node_modules/node-forge/lib/x509.js +2986 -0
  189. package/lib/node_modules/node-forge/lib/x509.js.map +1 -0
  190. package/lib/node_modules/prop-types/index.js +1 -1
  191. package/lib/node_modules/react-async-script/lib/esm/async-script-loader.js +1 -1
  192. package/lib/node_modules/react-google-recaptcha/lib/esm/recaptcha.js +1 -1
  193. package/lib/node_modules/react-is/index.js +1 -1
  194. package/lib/node_modules/react-phone-number-input/modules/CountryIcon.js +1 -1
  195. package/lib/node_modules/react-phone-number-input/modules/CountrySelect.js +1 -1
  196. package/lib/node_modules/react-phone-number-input/modules/Flag.js +1 -1
  197. package/lib/node_modules/react-phone-number-input/modules/InputBasic.js +1 -1
  198. package/lib/node_modules/react-phone-number-input/modules/InputSmart.js +1 -1
  199. package/lib/node_modules/react-phone-number-input/modules/InternationalIcon.js +1 -1
  200. package/lib/node_modules/react-phone-number-input/modules/PhoneInputWithCountry.js +1 -1
  201. package/lib/node_modules/react-phone-number-input/modules/PropTypes.js +1 -1
  202. package/lib/node_modules/scheduler/index.js +1 -1
  203. package/lib/node_modules/use-context-selector/dist/index.js +1 -1
  204. package/lib/styles.css +1 -1
  205. package/lib/styles.css.map +1 -1
  206. package/lib/utils/getEncryptedData/getEncryptedData.js +10 -13
  207. package/lib/utils/getEncryptedData/getEncryptedData.js.map +1 -1
  208. package/package.json +3 -1
@@ -0,0 +1,2565 @@
1
+ import { commonjsGlobal } from '../../../_virtual/_commonjsHelpers.js';
2
+ import { __module as util } from '../../../_virtual/util.js';
3
+ import { __require as requireForge } from './forge.js';
4
+ import { __require as requireBaseN } from './baseN.js';
5
+
6
+ /**
7
+ * Utility functions for web applications.
8
+ *
9
+ * @author Dave Longley
10
+ *
11
+ * Copyright (c) 2010-2018 Digital Bazaar, Inc.
12
+ */
13
+ var hasRequiredUtil;
14
+ function requireUtil() {
15
+ if (hasRequiredUtil) return util.exports;
16
+ hasRequiredUtil = 1;
17
+ var forge = requireForge();
18
+ var baseN = requireBaseN();
19
+
20
+ /* Utilities API */
21
+ var util$1 = util.exports = forge.util = forge.util || {};
22
+
23
+ // define setImmediate and nextTick
24
+ (function () {
25
+ // use native nextTick (unless we're in webpack)
26
+ // webpack (or better node-libs-browser polyfill) sets process.browser.
27
+ // this way we can detect webpack properly
28
+ if (typeof process !== 'undefined' && process.nextTick && !process.browser) {
29
+ util$1.nextTick = process.nextTick;
30
+ if (typeof setImmediate === 'function') {
31
+ util$1.setImmediate = setImmediate;
32
+ } else {
33
+ // polyfill setImmediate with nextTick, older versions of node
34
+ // (those w/o setImmediate) won't totally starve IO
35
+ util$1.setImmediate = util$1.nextTick;
36
+ }
37
+ return;
38
+ }
39
+
40
+ // polyfill nextTick with native setImmediate
41
+ if (typeof setImmediate === 'function') {
42
+ util$1.setImmediate = function () {
43
+ return setImmediate.apply(undefined, arguments);
44
+ };
45
+ util$1.nextTick = function (callback) {
46
+ return setImmediate(callback);
47
+ };
48
+ return;
49
+ }
50
+
51
+ /* Note: A polyfill upgrade pattern is used here to allow combining
52
+ polyfills. For example, MutationObserver is fast, but blocks UI updates,
53
+ so it needs to allow UI updates periodically, so it falls back on
54
+ postMessage or setTimeout. */
55
+
56
+ // polyfill with setTimeout
57
+ util$1.setImmediate = function (callback) {
58
+ setTimeout(callback, 0);
59
+ };
60
+
61
+ // upgrade polyfill to use postMessage
62
+ if (typeof window !== 'undefined' && typeof window.postMessage === 'function') {
63
+ var msg = 'forge.setImmediate';
64
+ var callbacks = [];
65
+ util$1.setImmediate = function (callback) {
66
+ callbacks.push(callback);
67
+ // only send message when one hasn't been sent in
68
+ // the current turn of the event loop
69
+ if (callbacks.length === 1) {
70
+ window.postMessage(msg, '*');
71
+ }
72
+ };
73
+ function handler(event) {
74
+ if (event.source === window && event.data === msg) {
75
+ event.stopPropagation();
76
+ var copy = callbacks.slice();
77
+ callbacks.length = 0;
78
+ copy.forEach(function (callback) {
79
+ callback();
80
+ });
81
+ }
82
+ }
83
+ window.addEventListener('message', handler, true);
84
+ }
85
+
86
+ // upgrade polyfill to use MutationObserver
87
+ if (typeof MutationObserver !== 'undefined') {
88
+ // polyfill with MutationObserver
89
+ var now = Date.now();
90
+ var attr = true;
91
+ var div = document.createElement('div');
92
+ var callbacks = [];
93
+ new MutationObserver(function () {
94
+ var copy = callbacks.slice();
95
+ callbacks.length = 0;
96
+ copy.forEach(function (callback) {
97
+ callback();
98
+ });
99
+ }).observe(div, {
100
+ attributes: true
101
+ });
102
+ var oldSetImmediate = util$1.setImmediate;
103
+ util$1.setImmediate = function (callback) {
104
+ if (Date.now() - now > 15) {
105
+ now = Date.now();
106
+ oldSetImmediate(callback);
107
+ } else {
108
+ callbacks.push(callback);
109
+ // only trigger observer when it hasn't been triggered in
110
+ // the current turn of the event loop
111
+ if (callbacks.length === 1) {
112
+ div.setAttribute('a', attr = !attr);
113
+ }
114
+ }
115
+ };
116
+ }
117
+ util$1.nextTick = util$1.setImmediate;
118
+ })();
119
+
120
+ // check if running under Node.js
121
+ util$1.isNodejs = typeof process !== 'undefined' && process.versions && process.versions.node;
122
+
123
+ // 'self' will also work in Web Workers (instance of WorkerGlobalScope) while
124
+ // it will point to `window` in the main thread.
125
+ // To remain compatible with older browsers, we fall back to 'window' if 'self'
126
+ // is not available.
127
+ util$1.globalScope = function () {
128
+ if (util$1.isNodejs) {
129
+ return commonjsGlobal;
130
+ }
131
+ return typeof self === 'undefined' ? window : self;
132
+ }();
133
+
134
+ // define isArray
135
+ util$1.isArray = Array.isArray || function (x) {
136
+ return Object.prototype.toString.call(x) === '[object Array]';
137
+ };
138
+
139
+ // define isArrayBuffer
140
+ util$1.isArrayBuffer = function (x) {
141
+ return typeof ArrayBuffer !== 'undefined' && x instanceof ArrayBuffer;
142
+ };
143
+
144
+ // define isArrayBufferView
145
+ util$1.isArrayBufferView = function (x) {
146
+ return x && util$1.isArrayBuffer(x.buffer) && x.byteLength !== undefined;
147
+ };
148
+
149
+ /**
150
+ * Ensure a bits param is 8, 16, 24, or 32. Used to validate input for
151
+ * algorithms where bit manipulation, JavaScript limitations, and/or algorithm
152
+ * design only allow for byte operations of a limited size.
153
+ *
154
+ * @param n number of bits.
155
+ *
156
+ * Throw Error if n invalid.
157
+ */
158
+ function _checkBitsParam(n) {
159
+ if (!(n === 8 || n === 16 || n === 24 || n === 32)) {
160
+ throw new Error('Only 8, 16, 24, or 32 bits supported: ' + n);
161
+ }
162
+ }
163
+
164
+ // TODO: set ByteBuffer to best available backing
165
+ util$1.ByteBuffer = ByteStringBuffer;
166
+
167
+ /** Buffer w/BinaryString backing */
168
+
169
+ /**
170
+ * Constructor for a binary string backed byte buffer.
171
+ *
172
+ * @param [b] the bytes to wrap (either encoded as string, one byte per
173
+ * character, or as an ArrayBuffer or Typed Array).
174
+ */
175
+ function ByteStringBuffer(b) {
176
+ // TODO: update to match DataBuffer API
177
+
178
+ // the data in this buffer
179
+ this.data = '';
180
+ // the pointer for reading from this buffer
181
+ this.read = 0;
182
+ if (typeof b === 'string') {
183
+ this.data = b;
184
+ } else if (util$1.isArrayBuffer(b) || util$1.isArrayBufferView(b)) {
185
+ if (typeof Buffer !== 'undefined' && b instanceof Buffer) {
186
+ this.data = b.toString('binary');
187
+ } else {
188
+ // convert native buffer to forge buffer
189
+ // FIXME: support native buffers internally instead
190
+ var arr = new Uint8Array(b);
191
+ try {
192
+ this.data = String.fromCharCode.apply(null, arr);
193
+ } catch (e) {
194
+ for (var i = 0; i < arr.length; ++i) {
195
+ this.putByte(arr[i]);
196
+ }
197
+ }
198
+ }
199
+ } else if (b instanceof ByteStringBuffer || typeof b === 'object' && typeof b.data === 'string' && typeof b.read === 'number') {
200
+ // copy existing buffer
201
+ this.data = b.data;
202
+ this.read = b.read;
203
+ }
204
+
205
+ // used for v8 optimization
206
+ this._constructedStringLength = 0;
207
+ }
208
+ util$1.ByteStringBuffer = ByteStringBuffer;
209
+
210
+ /* Note: This is an optimization for V8-based browsers. When V8 concatenates
211
+ a string, the strings are only joined logically using a "cons string" or
212
+ "constructed/concatenated string". These containers keep references to one
213
+ another and can result in very large memory usage. For example, if a 2MB
214
+ string is constructed by concatenating 4 bytes together at a time, the
215
+ memory usage will be ~44MB; so ~22x increase. The strings are only joined
216
+ together when an operation requiring their joining takes place, such as
217
+ substr(). This function is called when adding data to this buffer to ensure
218
+ these types of strings are periodically joined to reduce the memory
219
+ footprint. */
220
+ var _MAX_CONSTRUCTED_STRING_LENGTH = 4096;
221
+ util$1.ByteStringBuffer.prototype._optimizeConstructedString = function (x) {
222
+ this._constructedStringLength += x;
223
+ if (this._constructedStringLength > _MAX_CONSTRUCTED_STRING_LENGTH) {
224
+ // this substr() should cause the constructed string to join
225
+ this.data.substr(0, 1);
226
+ this._constructedStringLength = 0;
227
+ }
228
+ };
229
+
230
+ /**
231
+ * Gets the number of bytes in this buffer.
232
+ *
233
+ * @return the number of bytes in this buffer.
234
+ */
235
+ util$1.ByteStringBuffer.prototype.length = function () {
236
+ return this.data.length - this.read;
237
+ };
238
+
239
+ /**
240
+ * Gets whether or not this buffer is empty.
241
+ *
242
+ * @return true if this buffer is empty, false if not.
243
+ */
244
+ util$1.ByteStringBuffer.prototype.isEmpty = function () {
245
+ return this.length() <= 0;
246
+ };
247
+
248
+ /**
249
+ * Puts a byte in this buffer.
250
+ *
251
+ * @param b the byte to put.
252
+ *
253
+ * @return this buffer.
254
+ */
255
+ util$1.ByteStringBuffer.prototype.putByte = function (b) {
256
+ return this.putBytes(String.fromCharCode(b));
257
+ };
258
+
259
+ /**
260
+ * Puts a byte in this buffer N times.
261
+ *
262
+ * @param b the byte to put.
263
+ * @param n the number of bytes of value b to put.
264
+ *
265
+ * @return this buffer.
266
+ */
267
+ util$1.ByteStringBuffer.prototype.fillWithByte = function (b, n) {
268
+ b = String.fromCharCode(b);
269
+ var d = this.data;
270
+ while (n > 0) {
271
+ if (n & 1) {
272
+ d += b;
273
+ }
274
+ n >>>= 1;
275
+ if (n > 0) {
276
+ b += b;
277
+ }
278
+ }
279
+ this.data = d;
280
+ this._optimizeConstructedString(n);
281
+ return this;
282
+ };
283
+
284
+ /**
285
+ * Puts bytes in this buffer.
286
+ *
287
+ * @param bytes the bytes (as a binary encoded string) to put.
288
+ *
289
+ * @return this buffer.
290
+ */
291
+ util$1.ByteStringBuffer.prototype.putBytes = function (bytes) {
292
+ this.data += bytes;
293
+ this._optimizeConstructedString(bytes.length);
294
+ return this;
295
+ };
296
+
297
+ /**
298
+ * Puts a UTF-16 encoded string into this buffer.
299
+ *
300
+ * @param str the string to put.
301
+ *
302
+ * @return this buffer.
303
+ */
304
+ util$1.ByteStringBuffer.prototype.putString = function (str) {
305
+ return this.putBytes(util$1.encodeUtf8(str));
306
+ };
307
+
308
+ /**
309
+ * Puts a 16-bit integer in this buffer in big-endian order.
310
+ *
311
+ * @param i the 16-bit integer.
312
+ *
313
+ * @return this buffer.
314
+ */
315
+ util$1.ByteStringBuffer.prototype.putInt16 = function (i) {
316
+ return this.putBytes(String.fromCharCode(i >> 8 & 0xFF) + String.fromCharCode(i & 0xFF));
317
+ };
318
+
319
+ /**
320
+ * Puts a 24-bit integer in this buffer in big-endian order.
321
+ *
322
+ * @param i the 24-bit integer.
323
+ *
324
+ * @return this buffer.
325
+ */
326
+ util$1.ByteStringBuffer.prototype.putInt24 = function (i) {
327
+ return this.putBytes(String.fromCharCode(i >> 16 & 0xFF) + String.fromCharCode(i >> 8 & 0xFF) + String.fromCharCode(i & 0xFF));
328
+ };
329
+
330
+ /**
331
+ * Puts a 32-bit integer in this buffer in big-endian order.
332
+ *
333
+ * @param i the 32-bit integer.
334
+ *
335
+ * @return this buffer.
336
+ */
337
+ util$1.ByteStringBuffer.prototype.putInt32 = function (i) {
338
+ return this.putBytes(String.fromCharCode(i >> 24 & 0xFF) + String.fromCharCode(i >> 16 & 0xFF) + String.fromCharCode(i >> 8 & 0xFF) + String.fromCharCode(i & 0xFF));
339
+ };
340
+
341
+ /**
342
+ * Puts a 16-bit integer in this buffer in little-endian order.
343
+ *
344
+ * @param i the 16-bit integer.
345
+ *
346
+ * @return this buffer.
347
+ */
348
+ util$1.ByteStringBuffer.prototype.putInt16Le = function (i) {
349
+ return this.putBytes(String.fromCharCode(i & 0xFF) + String.fromCharCode(i >> 8 & 0xFF));
350
+ };
351
+
352
+ /**
353
+ * Puts a 24-bit integer in this buffer in little-endian order.
354
+ *
355
+ * @param i the 24-bit integer.
356
+ *
357
+ * @return this buffer.
358
+ */
359
+ util$1.ByteStringBuffer.prototype.putInt24Le = function (i) {
360
+ return this.putBytes(String.fromCharCode(i & 0xFF) + String.fromCharCode(i >> 8 & 0xFF) + String.fromCharCode(i >> 16 & 0xFF));
361
+ };
362
+
363
+ /**
364
+ * Puts a 32-bit integer in this buffer in little-endian order.
365
+ *
366
+ * @param i the 32-bit integer.
367
+ *
368
+ * @return this buffer.
369
+ */
370
+ util$1.ByteStringBuffer.prototype.putInt32Le = function (i) {
371
+ return this.putBytes(String.fromCharCode(i & 0xFF) + String.fromCharCode(i >> 8 & 0xFF) + String.fromCharCode(i >> 16 & 0xFF) + String.fromCharCode(i >> 24 & 0xFF));
372
+ };
373
+
374
+ /**
375
+ * Puts an n-bit integer in this buffer in big-endian order.
376
+ *
377
+ * @param i the n-bit integer.
378
+ * @param n the number of bits in the integer (8, 16, 24, or 32).
379
+ *
380
+ * @return this buffer.
381
+ */
382
+ util$1.ByteStringBuffer.prototype.putInt = function (i, n) {
383
+ _checkBitsParam(n);
384
+ var bytes = '';
385
+ do {
386
+ n -= 8;
387
+ bytes += String.fromCharCode(i >> n & 0xFF);
388
+ } while (n > 0);
389
+ return this.putBytes(bytes);
390
+ };
391
+
392
+ /**
393
+ * Puts a signed n-bit integer in this buffer in big-endian order. Two's
394
+ * complement representation is used.
395
+ *
396
+ * @param i the n-bit integer.
397
+ * @param n the number of bits in the integer (8, 16, 24, or 32).
398
+ *
399
+ * @return this buffer.
400
+ */
401
+ util$1.ByteStringBuffer.prototype.putSignedInt = function (i, n) {
402
+ // putInt checks n
403
+ if (i < 0) {
404
+ i += 2 << n - 1;
405
+ }
406
+ return this.putInt(i, n);
407
+ };
408
+
409
+ /**
410
+ * Puts the given buffer into this buffer.
411
+ *
412
+ * @param buffer the buffer to put into this one.
413
+ *
414
+ * @return this buffer.
415
+ */
416
+ util$1.ByteStringBuffer.prototype.putBuffer = function (buffer) {
417
+ return this.putBytes(buffer.getBytes());
418
+ };
419
+
420
+ /**
421
+ * Gets a byte from this buffer and advances the read pointer by 1.
422
+ *
423
+ * @return the byte.
424
+ */
425
+ util$1.ByteStringBuffer.prototype.getByte = function () {
426
+ return this.data.charCodeAt(this.read++);
427
+ };
428
+
429
+ /**
430
+ * Gets a uint16 from this buffer in big-endian order and advances the read
431
+ * pointer by 2.
432
+ *
433
+ * @return the uint16.
434
+ */
435
+ util$1.ByteStringBuffer.prototype.getInt16 = function () {
436
+ var rval = this.data.charCodeAt(this.read) << 8 ^ this.data.charCodeAt(this.read + 1);
437
+ this.read += 2;
438
+ return rval;
439
+ };
440
+
441
+ /**
442
+ * Gets a uint24 from this buffer in big-endian order and advances the read
443
+ * pointer by 3.
444
+ *
445
+ * @return the uint24.
446
+ */
447
+ util$1.ByteStringBuffer.prototype.getInt24 = function () {
448
+ var rval = this.data.charCodeAt(this.read) << 16 ^ this.data.charCodeAt(this.read + 1) << 8 ^ this.data.charCodeAt(this.read + 2);
449
+ this.read += 3;
450
+ return rval;
451
+ };
452
+
453
+ /**
454
+ * Gets a uint32 from this buffer in big-endian order and advances the read
455
+ * pointer by 4.
456
+ *
457
+ * @return the word.
458
+ */
459
+ util$1.ByteStringBuffer.prototype.getInt32 = function () {
460
+ var rval = this.data.charCodeAt(this.read) << 24 ^ this.data.charCodeAt(this.read + 1) << 16 ^ this.data.charCodeAt(this.read + 2) << 8 ^ this.data.charCodeAt(this.read + 3);
461
+ this.read += 4;
462
+ return rval;
463
+ };
464
+
465
+ /**
466
+ * Gets a uint16 from this buffer in little-endian order and advances the read
467
+ * pointer by 2.
468
+ *
469
+ * @return the uint16.
470
+ */
471
+ util$1.ByteStringBuffer.prototype.getInt16Le = function () {
472
+ var rval = this.data.charCodeAt(this.read) ^ this.data.charCodeAt(this.read + 1) << 8;
473
+ this.read += 2;
474
+ return rval;
475
+ };
476
+
477
+ /**
478
+ * Gets a uint24 from this buffer in little-endian order and advances the read
479
+ * pointer by 3.
480
+ *
481
+ * @return the uint24.
482
+ */
483
+ util$1.ByteStringBuffer.prototype.getInt24Le = function () {
484
+ var rval = this.data.charCodeAt(this.read) ^ this.data.charCodeAt(this.read + 1) << 8 ^ this.data.charCodeAt(this.read + 2) << 16;
485
+ this.read += 3;
486
+ return rval;
487
+ };
488
+
489
+ /**
490
+ * Gets a uint32 from this buffer in little-endian order and advances the read
491
+ * pointer by 4.
492
+ *
493
+ * @return the word.
494
+ */
495
+ util$1.ByteStringBuffer.prototype.getInt32Le = function () {
496
+ var rval = this.data.charCodeAt(this.read) ^ this.data.charCodeAt(this.read + 1) << 8 ^ this.data.charCodeAt(this.read + 2) << 16 ^ this.data.charCodeAt(this.read + 3) << 24;
497
+ this.read += 4;
498
+ return rval;
499
+ };
500
+
501
+ /**
502
+ * Gets an n-bit integer from this buffer in big-endian order and advances the
503
+ * read pointer by ceil(n/8).
504
+ *
505
+ * @param n the number of bits in the integer (8, 16, 24, or 32).
506
+ *
507
+ * @return the integer.
508
+ */
509
+ util$1.ByteStringBuffer.prototype.getInt = function (n) {
510
+ _checkBitsParam(n);
511
+ var rval = 0;
512
+ do {
513
+ // TODO: Use (rval * 0x100) if adding support for 33 to 53 bits.
514
+ rval = (rval << 8) + this.data.charCodeAt(this.read++);
515
+ n -= 8;
516
+ } while (n > 0);
517
+ return rval;
518
+ };
519
+
520
+ /**
521
+ * Gets a signed n-bit integer from this buffer in big-endian order, using
522
+ * two's complement, and advances the read pointer by n/8.
523
+ *
524
+ * @param n the number of bits in the integer (8, 16, 24, or 32).
525
+ *
526
+ * @return the integer.
527
+ */
528
+ util$1.ByteStringBuffer.prototype.getSignedInt = function (n) {
529
+ // getInt checks n
530
+ var x = this.getInt(n);
531
+ var max = 2 << n - 2;
532
+ if (x >= max) {
533
+ x -= max << 1;
534
+ }
535
+ return x;
536
+ };
537
+
538
+ /**
539
+ * Reads bytes out as a binary encoded string and clears them from the
540
+ * buffer. Note that the resulting string is binary encoded (in node.js this
541
+ * encoding is referred to as `binary`, it is *not* `utf8`).
542
+ *
543
+ * @param count the number of bytes to read, undefined or null for all.
544
+ *
545
+ * @return a binary encoded string of bytes.
546
+ */
547
+ util$1.ByteStringBuffer.prototype.getBytes = function (count) {
548
+ var rval;
549
+ if (count) {
550
+ // read count bytes
551
+ count = Math.min(this.length(), count);
552
+ rval = this.data.slice(this.read, this.read + count);
553
+ this.read += count;
554
+ } else if (count === 0) {
555
+ rval = '';
556
+ } else {
557
+ // read all bytes, optimize to only copy when needed
558
+ rval = this.read === 0 ? this.data : this.data.slice(this.read);
559
+ this.clear();
560
+ }
561
+ return rval;
562
+ };
563
+
564
+ /**
565
+ * Gets a binary encoded string of the bytes from this buffer without
566
+ * modifying the read pointer.
567
+ *
568
+ * @param count the number of bytes to get, omit to get all.
569
+ *
570
+ * @return a string full of binary encoded characters.
571
+ */
572
+ util$1.ByteStringBuffer.prototype.bytes = function (count) {
573
+ return typeof count === 'undefined' ? this.data.slice(this.read) : this.data.slice(this.read, this.read + count);
574
+ };
575
+
576
+ /**
577
+ * Gets a byte at the given index without modifying the read pointer.
578
+ *
579
+ * @param i the byte index.
580
+ *
581
+ * @return the byte.
582
+ */
583
+ util$1.ByteStringBuffer.prototype.at = function (i) {
584
+ return this.data.charCodeAt(this.read + i);
585
+ };
586
+
587
+ /**
588
+ * Puts a byte at the given index without modifying the read pointer.
589
+ *
590
+ * @param i the byte index.
591
+ * @param b the byte to put.
592
+ *
593
+ * @return this buffer.
594
+ */
595
+ util$1.ByteStringBuffer.prototype.setAt = function (i, b) {
596
+ this.data = this.data.substr(0, this.read + i) + String.fromCharCode(b) + this.data.substr(this.read + i + 1);
597
+ return this;
598
+ };
599
+
600
+ /**
601
+ * Gets the last byte without modifying the read pointer.
602
+ *
603
+ * @return the last byte.
604
+ */
605
+ util$1.ByteStringBuffer.prototype.last = function () {
606
+ return this.data.charCodeAt(this.data.length - 1);
607
+ };
608
+
609
+ /**
610
+ * Creates a copy of this buffer.
611
+ *
612
+ * @return the copy.
613
+ */
614
+ util$1.ByteStringBuffer.prototype.copy = function () {
615
+ var c = util$1.createBuffer(this.data);
616
+ c.read = this.read;
617
+ return c;
618
+ };
619
+
620
+ /**
621
+ * Compacts this buffer.
622
+ *
623
+ * @return this buffer.
624
+ */
625
+ util$1.ByteStringBuffer.prototype.compact = function () {
626
+ if (this.read > 0) {
627
+ this.data = this.data.slice(this.read);
628
+ this.read = 0;
629
+ }
630
+ return this;
631
+ };
632
+
633
+ /**
634
+ * Clears this buffer.
635
+ *
636
+ * @return this buffer.
637
+ */
638
+ util$1.ByteStringBuffer.prototype.clear = function () {
639
+ this.data = '';
640
+ this.read = 0;
641
+ return this;
642
+ };
643
+
644
+ /**
645
+ * Shortens this buffer by triming bytes off of the end of this buffer.
646
+ *
647
+ * @param count the number of bytes to trim off.
648
+ *
649
+ * @return this buffer.
650
+ */
651
+ util$1.ByteStringBuffer.prototype.truncate = function (count) {
652
+ var len = Math.max(0, this.length() - count);
653
+ this.data = this.data.substr(this.read, len);
654
+ this.read = 0;
655
+ return this;
656
+ };
657
+
658
+ /**
659
+ * Converts this buffer to a hexadecimal string.
660
+ *
661
+ * @return a hexadecimal string.
662
+ */
663
+ util$1.ByteStringBuffer.prototype.toHex = function () {
664
+ var rval = '';
665
+ for (var i = this.read; i < this.data.length; ++i) {
666
+ var b = this.data.charCodeAt(i);
667
+ if (b < 16) {
668
+ rval += '0';
669
+ }
670
+ rval += b.toString(16);
671
+ }
672
+ return rval;
673
+ };
674
+
675
+ /**
676
+ * Converts this buffer to a UTF-16 string (standard JavaScript string).
677
+ *
678
+ * @return a UTF-16 string.
679
+ */
680
+ util$1.ByteStringBuffer.prototype.toString = function () {
681
+ return util$1.decodeUtf8(this.bytes());
682
+ };
683
+
684
+ /** End Buffer w/BinaryString backing */
685
+
686
+ /** Buffer w/UInt8Array backing */
687
+
688
+ /**
689
+ * FIXME: Experimental. Do not use yet.
690
+ *
691
+ * Constructor for an ArrayBuffer-backed byte buffer.
692
+ *
693
+ * The buffer may be constructed from a string, an ArrayBuffer, DataView, or a
694
+ * TypedArray.
695
+ *
696
+ * If a string is given, its encoding should be provided as an option,
697
+ * otherwise it will default to 'binary'. A 'binary' string is encoded such
698
+ * that each character is one byte in length and size.
699
+ *
700
+ * If an ArrayBuffer, DataView, or TypedArray is given, it will be used
701
+ * *directly* without any copying. Note that, if a write to the buffer requires
702
+ * more space, the buffer will allocate a new backing ArrayBuffer to
703
+ * accommodate. The starting read and write offsets for the buffer may be
704
+ * given as options.
705
+ *
706
+ * @param [b] the initial bytes for this buffer.
707
+ * @param options the options to use:
708
+ * [readOffset] the starting read offset to use (default: 0).
709
+ * [writeOffset] the starting write offset to use (default: the
710
+ * length of the first parameter).
711
+ * [growSize] the minimum amount, in bytes, to grow the buffer by to
712
+ * accommodate writes (default: 1024).
713
+ * [encoding] the encoding ('binary', 'utf8', 'utf16', 'hex') for the
714
+ * first parameter, if it is a string (default: 'binary').
715
+ */
716
+ function DataBuffer(b, options) {
717
+ // default options
718
+ options = options || {};
719
+
720
+ // pointers for read from/write to buffer
721
+ this.read = options.readOffset || 0;
722
+ this.growSize = options.growSize || 1024;
723
+ var isArrayBuffer = util$1.isArrayBuffer(b);
724
+ var isArrayBufferView = util$1.isArrayBufferView(b);
725
+ if (isArrayBuffer || isArrayBufferView) {
726
+ // use ArrayBuffer directly
727
+ if (isArrayBuffer) {
728
+ this.data = new DataView(b);
729
+ } else {
730
+ // TODO: adjust read/write offset based on the type of view
731
+ // or specify that this must be done in the options ... that the
732
+ // offsets are byte-based
733
+ this.data = new DataView(b.buffer, b.byteOffset, b.byteLength);
734
+ }
735
+ this.write = 'writeOffset' in options ? options.writeOffset : this.data.byteLength;
736
+ return;
737
+ }
738
+
739
+ // initialize to empty array buffer and add any given bytes using putBytes
740
+ this.data = new DataView(new ArrayBuffer(0));
741
+ this.write = 0;
742
+ if (b !== null && b !== undefined) {
743
+ this.putBytes(b);
744
+ }
745
+ if ('writeOffset' in options) {
746
+ this.write = options.writeOffset;
747
+ }
748
+ }
749
+ util$1.DataBuffer = DataBuffer;
750
+
751
+ /**
752
+ * Gets the number of bytes in this buffer.
753
+ *
754
+ * @return the number of bytes in this buffer.
755
+ */
756
+ util$1.DataBuffer.prototype.length = function () {
757
+ return this.write - this.read;
758
+ };
759
+
760
+ /**
761
+ * Gets whether or not this buffer is empty.
762
+ *
763
+ * @return true if this buffer is empty, false if not.
764
+ */
765
+ util$1.DataBuffer.prototype.isEmpty = function () {
766
+ return this.length() <= 0;
767
+ };
768
+
769
+ /**
770
+ * Ensures this buffer has enough empty space to accommodate the given number
771
+ * of bytes. An optional parameter may be given that indicates a minimum
772
+ * amount to grow the buffer if necessary. If the parameter is not given,
773
+ * the buffer will be grown by some previously-specified default amount
774
+ * or heuristic.
775
+ *
776
+ * @param amount the number of bytes to accommodate.
777
+ * @param [growSize] the minimum amount, in bytes, to grow the buffer by if
778
+ * necessary.
779
+ */
780
+ util$1.DataBuffer.prototype.accommodate = function (amount, growSize) {
781
+ if (this.length() >= amount) {
782
+ return this;
783
+ }
784
+ growSize = Math.max(growSize || this.growSize, amount);
785
+
786
+ // grow buffer
787
+ var src = new Uint8Array(this.data.buffer, this.data.byteOffset, this.data.byteLength);
788
+ var dst = new Uint8Array(this.length() + growSize);
789
+ dst.set(src);
790
+ this.data = new DataView(dst.buffer);
791
+ return this;
792
+ };
793
+
794
+ /**
795
+ * Puts a byte in this buffer.
796
+ *
797
+ * @param b the byte to put.
798
+ *
799
+ * @return this buffer.
800
+ */
801
+ util$1.DataBuffer.prototype.putByte = function (b) {
802
+ this.accommodate(1);
803
+ this.data.setUint8(this.write++, b);
804
+ return this;
805
+ };
806
+
807
+ /**
808
+ * Puts a byte in this buffer N times.
809
+ *
810
+ * @param b the byte to put.
811
+ * @param n the number of bytes of value b to put.
812
+ *
813
+ * @return this buffer.
814
+ */
815
+ util$1.DataBuffer.prototype.fillWithByte = function (b, n) {
816
+ this.accommodate(n);
817
+ for (var i = 0; i < n; ++i) {
818
+ this.data.setUint8(b);
819
+ }
820
+ return this;
821
+ };
822
+
823
+ /**
824
+ * Puts bytes in this buffer. The bytes may be given as a string, an
825
+ * ArrayBuffer, a DataView, or a TypedArray.
826
+ *
827
+ * @param bytes the bytes to put.
828
+ * @param [encoding] the encoding for the first parameter ('binary', 'utf8',
829
+ * 'utf16', 'hex'), if it is a string (default: 'binary').
830
+ *
831
+ * @return this buffer.
832
+ */
833
+ util$1.DataBuffer.prototype.putBytes = function (bytes, encoding) {
834
+ if (util$1.isArrayBufferView(bytes)) {
835
+ var src = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
836
+ var len = src.byteLength - src.byteOffset;
837
+ this.accommodate(len);
838
+ var dst = new Uint8Array(this.data.buffer, this.write);
839
+ dst.set(src);
840
+ this.write += len;
841
+ return this;
842
+ }
843
+ if (util$1.isArrayBuffer(bytes)) {
844
+ var src = new Uint8Array(bytes);
845
+ this.accommodate(src.byteLength);
846
+ var dst = new Uint8Array(this.data.buffer);
847
+ dst.set(src, this.write);
848
+ this.write += src.byteLength;
849
+ return this;
850
+ }
851
+
852
+ // bytes is a util.DataBuffer or equivalent
853
+ if (bytes instanceof util$1.DataBuffer || typeof bytes === 'object' && typeof bytes.read === 'number' && typeof bytes.write === 'number' && util$1.isArrayBufferView(bytes.data)) {
854
+ var src = new Uint8Array(bytes.data.byteLength, bytes.read, bytes.length());
855
+ this.accommodate(src.byteLength);
856
+ var dst = new Uint8Array(bytes.data.byteLength, this.write);
857
+ dst.set(src);
858
+ this.write += src.byteLength;
859
+ return this;
860
+ }
861
+ if (bytes instanceof util$1.ByteStringBuffer) {
862
+ // copy binary string and process as the same as a string parameter below
863
+ bytes = bytes.data;
864
+ encoding = 'binary';
865
+ }
866
+
867
+ // string conversion
868
+ encoding = encoding || 'binary';
869
+ if (typeof bytes === 'string') {
870
+ var view;
871
+
872
+ // decode from string
873
+ if (encoding === 'hex') {
874
+ this.accommodate(Math.ceil(bytes.length / 2));
875
+ view = new Uint8Array(this.data.buffer, this.write);
876
+ this.write += util$1.binary.hex.decode(bytes, view, this.write);
877
+ return this;
878
+ }
879
+ if (encoding === 'base64') {
880
+ this.accommodate(Math.ceil(bytes.length / 4) * 3);
881
+ view = new Uint8Array(this.data.buffer, this.write);
882
+ this.write += util$1.binary.base64.decode(bytes, view, this.write);
883
+ return this;
884
+ }
885
+
886
+ // encode text as UTF-8 bytes
887
+ if (encoding === 'utf8') {
888
+ // encode as UTF-8 then decode string as raw binary
889
+ bytes = util$1.encodeUtf8(bytes);
890
+ encoding = 'binary';
891
+ }
892
+
893
+ // decode string as raw binary
894
+ if (encoding === 'binary' || encoding === 'raw') {
895
+ // one byte per character
896
+ this.accommodate(bytes.length);
897
+ view = new Uint8Array(this.data.buffer, this.write);
898
+ this.write += util$1.binary.raw.decode(view);
899
+ return this;
900
+ }
901
+
902
+ // encode text as UTF-16 bytes
903
+ if (encoding === 'utf16') {
904
+ // two bytes per character
905
+ this.accommodate(bytes.length * 2);
906
+ view = new Uint16Array(this.data.buffer, this.write);
907
+ this.write += util$1.text.utf16.encode(view);
908
+ return this;
909
+ }
910
+ throw new Error('Invalid encoding: ' + encoding);
911
+ }
912
+ throw Error('Invalid parameter: ' + bytes);
913
+ };
914
+
915
+ /**
916
+ * Puts the given buffer into this buffer.
917
+ *
918
+ * @param buffer the buffer to put into this one.
919
+ *
920
+ * @return this buffer.
921
+ */
922
+ util$1.DataBuffer.prototype.putBuffer = function (buffer) {
923
+ this.putBytes(buffer);
924
+ buffer.clear();
925
+ return this;
926
+ };
927
+
928
+ /**
929
+ * Puts a string into this buffer.
930
+ *
931
+ * @param str the string to put.
932
+ * @param [encoding] the encoding for the string (default: 'utf16').
933
+ *
934
+ * @return this buffer.
935
+ */
936
+ util$1.DataBuffer.prototype.putString = function (str) {
937
+ return this.putBytes(str, 'utf16');
938
+ };
939
+
940
+ /**
941
+ * Puts a 16-bit integer in this buffer in big-endian order.
942
+ *
943
+ * @param i the 16-bit integer.
944
+ *
945
+ * @return this buffer.
946
+ */
947
+ util$1.DataBuffer.prototype.putInt16 = function (i) {
948
+ this.accommodate(2);
949
+ this.data.setInt16(this.write, i);
950
+ this.write += 2;
951
+ return this;
952
+ };
953
+
954
+ /**
955
+ * Puts a 24-bit integer in this buffer in big-endian order.
956
+ *
957
+ * @param i the 24-bit integer.
958
+ *
959
+ * @return this buffer.
960
+ */
961
+ util$1.DataBuffer.prototype.putInt24 = function (i) {
962
+ this.accommodate(3);
963
+ this.data.setInt16(this.write, i >> 8 & 0xFFFF);
964
+ this.data.setInt8(this.write, i >> 16 & 0xFF);
965
+ this.write += 3;
966
+ return this;
967
+ };
968
+
969
+ /**
970
+ * Puts a 32-bit integer in this buffer in big-endian order.
971
+ *
972
+ * @param i the 32-bit integer.
973
+ *
974
+ * @return this buffer.
975
+ */
976
+ util$1.DataBuffer.prototype.putInt32 = function (i) {
977
+ this.accommodate(4);
978
+ this.data.setInt32(this.write, i);
979
+ this.write += 4;
980
+ return this;
981
+ };
982
+
983
+ /**
984
+ * Puts a 16-bit integer in this buffer in little-endian order.
985
+ *
986
+ * @param i the 16-bit integer.
987
+ *
988
+ * @return this buffer.
989
+ */
990
+ util$1.DataBuffer.prototype.putInt16Le = function (i) {
991
+ this.accommodate(2);
992
+ this.data.setInt16(this.write, i, true);
993
+ this.write += 2;
994
+ return this;
995
+ };
996
+
997
+ /**
998
+ * Puts a 24-bit integer in this buffer in little-endian order.
999
+ *
1000
+ * @param i the 24-bit integer.
1001
+ *
1002
+ * @return this buffer.
1003
+ */
1004
+ util$1.DataBuffer.prototype.putInt24Le = function (i) {
1005
+ this.accommodate(3);
1006
+ this.data.setInt8(this.write, i >> 16 & 0xFF);
1007
+ this.data.setInt16(this.write, i >> 8 & 0xFFFF, true);
1008
+ this.write += 3;
1009
+ return this;
1010
+ };
1011
+
1012
+ /**
1013
+ * Puts a 32-bit integer in this buffer in little-endian order.
1014
+ *
1015
+ * @param i the 32-bit integer.
1016
+ *
1017
+ * @return this buffer.
1018
+ */
1019
+ util$1.DataBuffer.prototype.putInt32Le = function (i) {
1020
+ this.accommodate(4);
1021
+ this.data.setInt32(this.write, i, true);
1022
+ this.write += 4;
1023
+ return this;
1024
+ };
1025
+
1026
+ /**
1027
+ * Puts an n-bit integer in this buffer in big-endian order.
1028
+ *
1029
+ * @param i the n-bit integer.
1030
+ * @param n the number of bits in the integer (8, 16, 24, or 32).
1031
+ *
1032
+ * @return this buffer.
1033
+ */
1034
+ util$1.DataBuffer.prototype.putInt = function (i, n) {
1035
+ _checkBitsParam(n);
1036
+ this.accommodate(n / 8);
1037
+ do {
1038
+ n -= 8;
1039
+ this.data.setInt8(this.write++, i >> n & 0xFF);
1040
+ } while (n > 0);
1041
+ return this;
1042
+ };
1043
+
1044
+ /**
1045
+ * Puts a signed n-bit integer in this buffer in big-endian order. Two's
1046
+ * complement representation is used.
1047
+ *
1048
+ * @param i the n-bit integer.
1049
+ * @param n the number of bits in the integer.
1050
+ *
1051
+ * @return this buffer.
1052
+ */
1053
+ util$1.DataBuffer.prototype.putSignedInt = function (i, n) {
1054
+ _checkBitsParam(n);
1055
+ this.accommodate(n / 8);
1056
+ if (i < 0) {
1057
+ i += 2 << n - 1;
1058
+ }
1059
+ return this.putInt(i, n);
1060
+ };
1061
+
1062
+ /**
1063
+ * Gets a byte from this buffer and advances the read pointer by 1.
1064
+ *
1065
+ * @return the byte.
1066
+ */
1067
+ util$1.DataBuffer.prototype.getByte = function () {
1068
+ return this.data.getInt8(this.read++);
1069
+ };
1070
+
1071
+ /**
1072
+ * Gets a uint16 from this buffer in big-endian order and advances the read
1073
+ * pointer by 2.
1074
+ *
1075
+ * @return the uint16.
1076
+ */
1077
+ util$1.DataBuffer.prototype.getInt16 = function () {
1078
+ var rval = this.data.getInt16(this.read);
1079
+ this.read += 2;
1080
+ return rval;
1081
+ };
1082
+
1083
+ /**
1084
+ * Gets a uint24 from this buffer in big-endian order and advances the read
1085
+ * pointer by 3.
1086
+ *
1087
+ * @return the uint24.
1088
+ */
1089
+ util$1.DataBuffer.prototype.getInt24 = function () {
1090
+ var rval = this.data.getInt16(this.read) << 8 ^ this.data.getInt8(this.read + 2);
1091
+ this.read += 3;
1092
+ return rval;
1093
+ };
1094
+
1095
+ /**
1096
+ * Gets a uint32 from this buffer in big-endian order and advances the read
1097
+ * pointer by 4.
1098
+ *
1099
+ * @return the word.
1100
+ */
1101
+ util$1.DataBuffer.prototype.getInt32 = function () {
1102
+ var rval = this.data.getInt32(this.read);
1103
+ this.read += 4;
1104
+ return rval;
1105
+ };
1106
+
1107
+ /**
1108
+ * Gets a uint16 from this buffer in little-endian order and advances the read
1109
+ * pointer by 2.
1110
+ *
1111
+ * @return the uint16.
1112
+ */
1113
+ util$1.DataBuffer.prototype.getInt16Le = function () {
1114
+ var rval = this.data.getInt16(this.read, true);
1115
+ this.read += 2;
1116
+ return rval;
1117
+ };
1118
+
1119
+ /**
1120
+ * Gets a uint24 from this buffer in little-endian order and advances the read
1121
+ * pointer by 3.
1122
+ *
1123
+ * @return the uint24.
1124
+ */
1125
+ util$1.DataBuffer.prototype.getInt24Le = function () {
1126
+ var rval = this.data.getInt8(this.read) ^ this.data.getInt16(this.read + 1, true) << 8;
1127
+ this.read += 3;
1128
+ return rval;
1129
+ };
1130
+
1131
+ /**
1132
+ * Gets a uint32 from this buffer in little-endian order and advances the read
1133
+ * pointer by 4.
1134
+ *
1135
+ * @return the word.
1136
+ */
1137
+ util$1.DataBuffer.prototype.getInt32Le = function () {
1138
+ var rval = this.data.getInt32(this.read, true);
1139
+ this.read += 4;
1140
+ return rval;
1141
+ };
1142
+
1143
+ /**
1144
+ * Gets an n-bit integer from this buffer in big-endian order and advances the
1145
+ * read pointer by n/8.
1146
+ *
1147
+ * @param n the number of bits in the integer (8, 16, 24, or 32).
1148
+ *
1149
+ * @return the integer.
1150
+ */
1151
+ util$1.DataBuffer.prototype.getInt = function (n) {
1152
+ _checkBitsParam(n);
1153
+ var rval = 0;
1154
+ do {
1155
+ // TODO: Use (rval * 0x100) if adding support for 33 to 53 bits.
1156
+ rval = (rval << 8) + this.data.getInt8(this.read++);
1157
+ n -= 8;
1158
+ } while (n > 0);
1159
+ return rval;
1160
+ };
1161
+
1162
+ /**
1163
+ * Gets a signed n-bit integer from this buffer in big-endian order, using
1164
+ * two's complement, and advances the read pointer by n/8.
1165
+ *
1166
+ * @param n the number of bits in the integer (8, 16, 24, or 32).
1167
+ *
1168
+ * @return the integer.
1169
+ */
1170
+ util$1.DataBuffer.prototype.getSignedInt = function (n) {
1171
+ // getInt checks n
1172
+ var x = this.getInt(n);
1173
+ var max = 2 << n - 2;
1174
+ if (x >= max) {
1175
+ x -= max << 1;
1176
+ }
1177
+ return x;
1178
+ };
1179
+
1180
+ /**
1181
+ * Reads bytes out as a binary encoded string and clears them from the
1182
+ * buffer.
1183
+ *
1184
+ * @param count the number of bytes to read, undefined or null for all.
1185
+ *
1186
+ * @return a binary encoded string of bytes.
1187
+ */
1188
+ util$1.DataBuffer.prototype.getBytes = function (count) {
1189
+ // TODO: deprecate this method, it is poorly named and
1190
+ // this.toString('binary') replaces it
1191
+ // add a toTypedArray()/toArrayBuffer() function
1192
+ var rval;
1193
+ if (count) {
1194
+ // read count bytes
1195
+ count = Math.min(this.length(), count);
1196
+ rval = this.data.slice(this.read, this.read + count);
1197
+ this.read += count;
1198
+ } else if (count === 0) {
1199
+ rval = '';
1200
+ } else {
1201
+ // read all bytes, optimize to only copy when needed
1202
+ rval = this.read === 0 ? this.data : this.data.slice(this.read);
1203
+ this.clear();
1204
+ }
1205
+ return rval;
1206
+ };
1207
+
1208
+ /**
1209
+ * Gets a binary encoded string of the bytes from this buffer without
1210
+ * modifying the read pointer.
1211
+ *
1212
+ * @param count the number of bytes to get, omit to get all.
1213
+ *
1214
+ * @return a string full of binary encoded characters.
1215
+ */
1216
+ util$1.DataBuffer.prototype.bytes = function (count) {
1217
+ // TODO: deprecate this method, it is poorly named, add "getString()"
1218
+ return typeof count === 'undefined' ? this.data.slice(this.read) : this.data.slice(this.read, this.read + count);
1219
+ };
1220
+
1221
+ /**
1222
+ * Gets a byte at the given index without modifying the read pointer.
1223
+ *
1224
+ * @param i the byte index.
1225
+ *
1226
+ * @return the byte.
1227
+ */
1228
+ util$1.DataBuffer.prototype.at = function (i) {
1229
+ return this.data.getUint8(this.read + i);
1230
+ };
1231
+
1232
+ /**
1233
+ * Puts a byte at the given index without modifying the read pointer.
1234
+ *
1235
+ * @param i the byte index.
1236
+ * @param b the byte to put.
1237
+ *
1238
+ * @return this buffer.
1239
+ */
1240
+ util$1.DataBuffer.prototype.setAt = function (i, b) {
1241
+ this.data.setUint8(i, b);
1242
+ return this;
1243
+ };
1244
+
1245
+ /**
1246
+ * Gets the last byte without modifying the read pointer.
1247
+ *
1248
+ * @return the last byte.
1249
+ */
1250
+ util$1.DataBuffer.prototype.last = function () {
1251
+ return this.data.getUint8(this.write - 1);
1252
+ };
1253
+
1254
+ /**
1255
+ * Creates a copy of this buffer.
1256
+ *
1257
+ * @return the copy.
1258
+ */
1259
+ util$1.DataBuffer.prototype.copy = function () {
1260
+ return new util$1.DataBuffer(this);
1261
+ };
1262
+
1263
+ /**
1264
+ * Compacts this buffer.
1265
+ *
1266
+ * @return this buffer.
1267
+ */
1268
+ util$1.DataBuffer.prototype.compact = function () {
1269
+ if (this.read > 0) {
1270
+ var src = new Uint8Array(this.data.buffer, this.read);
1271
+ var dst = new Uint8Array(src.byteLength);
1272
+ dst.set(src);
1273
+ this.data = new DataView(dst);
1274
+ this.write -= this.read;
1275
+ this.read = 0;
1276
+ }
1277
+ return this;
1278
+ };
1279
+
1280
+ /**
1281
+ * Clears this buffer.
1282
+ *
1283
+ * @return this buffer.
1284
+ */
1285
+ util$1.DataBuffer.prototype.clear = function () {
1286
+ this.data = new DataView(new ArrayBuffer(0));
1287
+ this.read = this.write = 0;
1288
+ return this;
1289
+ };
1290
+
1291
+ /**
1292
+ * Shortens this buffer by triming bytes off of the end of this buffer.
1293
+ *
1294
+ * @param count the number of bytes to trim off.
1295
+ *
1296
+ * @return this buffer.
1297
+ */
1298
+ util$1.DataBuffer.prototype.truncate = function (count) {
1299
+ this.write = Math.max(0, this.length() - count);
1300
+ this.read = Math.min(this.read, this.write);
1301
+ return this;
1302
+ };
1303
+
1304
+ /**
1305
+ * Converts this buffer to a hexadecimal string.
1306
+ *
1307
+ * @return a hexadecimal string.
1308
+ */
1309
+ util$1.DataBuffer.prototype.toHex = function () {
1310
+ var rval = '';
1311
+ for (var i = this.read; i < this.data.byteLength; ++i) {
1312
+ var b = this.data.getUint8(i);
1313
+ if (b < 16) {
1314
+ rval += '0';
1315
+ }
1316
+ rval += b.toString(16);
1317
+ }
1318
+ return rval;
1319
+ };
1320
+
1321
+ /**
1322
+ * Converts this buffer to a string, using the given encoding. If no
1323
+ * encoding is given, 'utf8' (UTF-8) is used.
1324
+ *
1325
+ * @param [encoding] the encoding to use: 'binary', 'utf8', 'utf16', 'hex',
1326
+ * 'base64' (default: 'utf8').
1327
+ *
1328
+ * @return a string representation of the bytes in this buffer.
1329
+ */
1330
+ util$1.DataBuffer.prototype.toString = function (encoding) {
1331
+ var view = new Uint8Array(this.data, this.read, this.length());
1332
+ encoding = encoding || 'utf8';
1333
+
1334
+ // encode to string
1335
+ if (encoding === 'binary' || encoding === 'raw') {
1336
+ return util$1.binary.raw.encode(view);
1337
+ }
1338
+ if (encoding === 'hex') {
1339
+ return util$1.binary.hex.encode(view);
1340
+ }
1341
+ if (encoding === 'base64') {
1342
+ return util$1.binary.base64.encode(view);
1343
+ }
1344
+
1345
+ // decode to text
1346
+ if (encoding === 'utf8') {
1347
+ return util$1.text.utf8.decode(view);
1348
+ }
1349
+ if (encoding === 'utf16') {
1350
+ return util$1.text.utf16.decode(view);
1351
+ }
1352
+ throw new Error('Invalid encoding: ' + encoding);
1353
+ };
1354
+
1355
+ /** End Buffer w/UInt8Array backing */
1356
+
1357
+ /**
1358
+ * Creates a buffer that stores bytes. A value may be given to populate the
1359
+ * buffer with data. This value can either be string of encoded bytes or a
1360
+ * regular string of characters. When passing a string of binary encoded
1361
+ * bytes, the encoding `raw` should be given. This is also the default. When
1362
+ * passing a string of characters, the encoding `utf8` should be given.
1363
+ *
1364
+ * @param [input] a string with encoded bytes to store in the buffer.
1365
+ * @param [encoding] (default: 'raw', other: 'utf8').
1366
+ */
1367
+ util$1.createBuffer = function (input, encoding) {
1368
+ // TODO: deprecate, use new ByteBuffer() instead
1369
+ encoding = encoding || 'raw';
1370
+ if (input !== undefined && encoding === 'utf8') {
1371
+ input = util$1.encodeUtf8(input);
1372
+ }
1373
+ return new util$1.ByteBuffer(input);
1374
+ };
1375
+
1376
+ /**
1377
+ * Fills a string with a particular value. If you want the string to be a byte
1378
+ * string, pass in String.fromCharCode(theByte).
1379
+ *
1380
+ * @param c the character to fill the string with, use String.fromCharCode
1381
+ * to fill the string with a byte value.
1382
+ * @param n the number of characters of value c to fill with.
1383
+ *
1384
+ * @return the filled string.
1385
+ */
1386
+ util$1.fillString = function (c, n) {
1387
+ var s = '';
1388
+ while (n > 0) {
1389
+ if (n & 1) {
1390
+ s += c;
1391
+ }
1392
+ n >>>= 1;
1393
+ if (n > 0) {
1394
+ c += c;
1395
+ }
1396
+ }
1397
+ return s;
1398
+ };
1399
+
1400
+ /**
1401
+ * Performs a per byte XOR between two byte strings and returns the result as a
1402
+ * string of bytes.
1403
+ *
1404
+ * @param s1 first string of bytes.
1405
+ * @param s2 second string of bytes.
1406
+ * @param n the number of bytes to XOR.
1407
+ *
1408
+ * @return the XOR'd result.
1409
+ */
1410
+ util$1.xorBytes = function (s1, s2, n) {
1411
+ var s3 = '';
1412
+ var b = '';
1413
+ var t = '';
1414
+ var i = 0;
1415
+ var c = 0;
1416
+ for (; n > 0; --n, ++i) {
1417
+ b = s1.charCodeAt(i) ^ s2.charCodeAt(i);
1418
+ if (c >= 10) {
1419
+ s3 += t;
1420
+ t = '';
1421
+ c = 0;
1422
+ }
1423
+ t += String.fromCharCode(b);
1424
+ ++c;
1425
+ }
1426
+ s3 += t;
1427
+ return s3;
1428
+ };
1429
+
1430
+ /**
1431
+ * Converts a hex string into a 'binary' encoded string of bytes.
1432
+ *
1433
+ * @param hex the hexadecimal string to convert.
1434
+ *
1435
+ * @return the binary-encoded string of bytes.
1436
+ */
1437
+ util$1.hexToBytes = function (hex) {
1438
+ // TODO: deprecate: "Deprecated. Use util.binary.hex.decode instead."
1439
+ var rval = '';
1440
+ var i = 0;
1441
+ if (hex.length & 1 == 1) {
1442
+ // odd number of characters, convert first character alone
1443
+ i = 1;
1444
+ rval += String.fromCharCode(parseInt(hex[0], 16));
1445
+ }
1446
+ // convert 2 characters (1 byte) at a time
1447
+ for (; i < hex.length; i += 2) {
1448
+ rval += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
1449
+ }
1450
+ return rval;
1451
+ };
1452
+
1453
+ /**
1454
+ * Converts a 'binary' encoded string of bytes to hex.
1455
+ *
1456
+ * @param bytes the byte string to convert.
1457
+ *
1458
+ * @return the string of hexadecimal characters.
1459
+ */
1460
+ util$1.bytesToHex = function (bytes) {
1461
+ // TODO: deprecate: "Deprecated. Use util.binary.hex.encode instead."
1462
+ return util$1.createBuffer(bytes).toHex();
1463
+ };
1464
+
1465
+ /**
1466
+ * Converts an 32-bit integer to 4-big-endian byte string.
1467
+ *
1468
+ * @param i the integer.
1469
+ *
1470
+ * @return the byte string.
1471
+ */
1472
+ util$1.int32ToBytes = function (i) {
1473
+ return String.fromCharCode(i >> 24 & 0xFF) + String.fromCharCode(i >> 16 & 0xFF) + String.fromCharCode(i >> 8 & 0xFF) + String.fromCharCode(i & 0xFF);
1474
+ };
1475
+
1476
+ // base64 characters, reverse mapping
1477
+ var _base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
1478
+ var _base64Idx = [/*43 -43 = 0*/
1479
+ /*'+', 1, 2, 3,'/' */
1480
+ 62, -1, -1, -1, 63, /*'0','1','2','3','4','5','6','7','8','9' */
1481
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, /*15, 16, 17,'=', 19, 20, 21 */
1482
+ -1, -1, -1, 64, -1, -1, -1, /*65 - 43 = 22*/
1483
+ /*'A','B','C','D','E','F','G','H','I','J','K','L','M', */
1484
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, /*'N','O','P','Q','R','S','T','U','V','W','X','Y','Z' */
1485
+ 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, /*91 - 43 = 48 */
1486
+ /*48, 49, 50, 51, 52, 53 */
1487
+ -1, -1, -1, -1, -1, -1, /*97 - 43 = 54*/
1488
+ /*'a','b','c','d','e','f','g','h','i','j','k','l','m' */
1489
+ 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /*'n','o','p','q','r','s','t','u','v','w','x','y','z' */
1490
+ 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51];
1491
+
1492
+ // base58 characters (Bitcoin alphabet)
1493
+ var _base58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
1494
+
1495
+ /**
1496
+ * Base64 encodes a 'binary' encoded string of bytes.
1497
+ *
1498
+ * @param input the binary encoded string of bytes to base64-encode.
1499
+ * @param maxline the maximum number of encoded characters per line to use,
1500
+ * defaults to none.
1501
+ *
1502
+ * @return the base64-encoded output.
1503
+ */
1504
+ util$1.encode64 = function (input, maxline) {
1505
+ // TODO: deprecate: "Deprecated. Use util.binary.base64.encode instead."
1506
+ var line = '';
1507
+ var output = '';
1508
+ var chr1, chr2, chr3;
1509
+ var i = 0;
1510
+ while (i < input.length) {
1511
+ chr1 = input.charCodeAt(i++);
1512
+ chr2 = input.charCodeAt(i++);
1513
+ chr3 = input.charCodeAt(i++);
1514
+
1515
+ // encode 4 character group
1516
+ line += _base64.charAt(chr1 >> 2);
1517
+ line += _base64.charAt((chr1 & 3) << 4 | chr2 >> 4);
1518
+ if (isNaN(chr2)) {
1519
+ line += '==';
1520
+ } else {
1521
+ line += _base64.charAt((chr2 & 15) << 2 | chr3 >> 6);
1522
+ line += isNaN(chr3) ? '=' : _base64.charAt(chr3 & 63);
1523
+ }
1524
+ if (maxline && line.length > maxline) {
1525
+ output += line.substr(0, maxline) + '\r\n';
1526
+ line = line.substr(maxline);
1527
+ }
1528
+ }
1529
+ output += line;
1530
+ return output;
1531
+ };
1532
+
1533
+ /**
1534
+ * Base64 decodes a string into a 'binary' encoded string of bytes.
1535
+ *
1536
+ * @param input the base64-encoded input.
1537
+ *
1538
+ * @return the binary encoded string.
1539
+ */
1540
+ util$1.decode64 = function (input) {
1541
+ // TODO: deprecate: "Deprecated. Use util.binary.base64.decode instead."
1542
+
1543
+ // remove all non-base64 characters
1544
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
1545
+ var output = '';
1546
+ var enc1, enc2, enc3, enc4;
1547
+ var i = 0;
1548
+ while (i < input.length) {
1549
+ enc1 = _base64Idx[input.charCodeAt(i++) - 43];
1550
+ enc2 = _base64Idx[input.charCodeAt(i++) - 43];
1551
+ enc3 = _base64Idx[input.charCodeAt(i++) - 43];
1552
+ enc4 = _base64Idx[input.charCodeAt(i++) - 43];
1553
+ output += String.fromCharCode(enc1 << 2 | enc2 >> 4);
1554
+ if (enc3 !== 64) {
1555
+ // decoded at least 2 bytes
1556
+ output += String.fromCharCode((enc2 & 15) << 4 | enc3 >> 2);
1557
+ if (enc4 !== 64) {
1558
+ // decoded 3 bytes
1559
+ output += String.fromCharCode((enc3 & 3) << 6 | enc4);
1560
+ }
1561
+ }
1562
+ }
1563
+ return output;
1564
+ };
1565
+
1566
+ /**
1567
+ * Encodes the given string of characters (a standard JavaScript
1568
+ * string) as a binary encoded string where the bytes represent
1569
+ * a UTF-8 encoded string of characters. Non-ASCII characters will be
1570
+ * encoded as multiple bytes according to UTF-8.
1571
+ *
1572
+ * @param str a standard string of characters to encode.
1573
+ *
1574
+ * @return the binary encoded string.
1575
+ */
1576
+ util$1.encodeUtf8 = function (str) {
1577
+ return unescape(encodeURIComponent(str));
1578
+ };
1579
+
1580
+ /**
1581
+ * Decodes a binary encoded string that contains bytes that
1582
+ * represent a UTF-8 encoded string of characters -- into a
1583
+ * string of characters (a standard JavaScript string).
1584
+ *
1585
+ * @param str the binary encoded string to decode.
1586
+ *
1587
+ * @return the resulting standard string of characters.
1588
+ */
1589
+ util$1.decodeUtf8 = function (str) {
1590
+ return decodeURIComponent(escape(str));
1591
+ };
1592
+
1593
+ // binary encoding/decoding tools
1594
+ // FIXME: Experimental. Do not use yet.
1595
+ util$1.binary = {
1596
+ raw: {},
1597
+ hex: {},
1598
+ base64: {},
1599
+ base58: {},
1600
+ baseN: {
1601
+ encode: baseN.encode,
1602
+ decode: baseN.decode
1603
+ }
1604
+ };
1605
+
1606
+ /**
1607
+ * Encodes a Uint8Array as a binary-encoded string. This encoding uses
1608
+ * a value between 0 and 255 for each character.
1609
+ *
1610
+ * @param bytes the Uint8Array to encode.
1611
+ *
1612
+ * @return the binary-encoded string.
1613
+ */
1614
+ util$1.binary.raw.encode = function (bytes) {
1615
+ return String.fromCharCode.apply(null, bytes);
1616
+ };
1617
+
1618
+ /**
1619
+ * Decodes a binary-encoded string to a Uint8Array. This encoding uses
1620
+ * a value between 0 and 255 for each character.
1621
+ *
1622
+ * @param str the binary-encoded string to decode.
1623
+ * @param [output] an optional Uint8Array to write the output to; if it
1624
+ * is too small, an exception will be thrown.
1625
+ * @param [offset] the start offset for writing to the output (default: 0).
1626
+ *
1627
+ * @return the Uint8Array or the number of bytes written if output was given.
1628
+ */
1629
+ util$1.binary.raw.decode = function (str, output, offset) {
1630
+ var out = output;
1631
+ if (!out) {
1632
+ out = new Uint8Array(str.length);
1633
+ }
1634
+ offset = offset || 0;
1635
+ var j = offset;
1636
+ for (var i = 0; i < str.length; ++i) {
1637
+ out[j++] = str.charCodeAt(i);
1638
+ }
1639
+ return output ? j - offset : out;
1640
+ };
1641
+
1642
+ /**
1643
+ * Encodes a 'binary' string, ArrayBuffer, DataView, TypedArray, or
1644
+ * ByteBuffer as a string of hexadecimal characters.
1645
+ *
1646
+ * @param bytes the bytes to convert.
1647
+ *
1648
+ * @return the string of hexadecimal characters.
1649
+ */
1650
+ util$1.binary.hex.encode = util$1.bytesToHex;
1651
+
1652
+ /**
1653
+ * Decodes a hex-encoded string to a Uint8Array.
1654
+ *
1655
+ * @param hex the hexadecimal string to convert.
1656
+ * @param [output] an optional Uint8Array to write the output to; if it
1657
+ * is too small, an exception will be thrown.
1658
+ * @param [offset] the start offset for writing to the output (default: 0).
1659
+ *
1660
+ * @return the Uint8Array or the number of bytes written if output was given.
1661
+ */
1662
+ util$1.binary.hex.decode = function (hex, output, offset) {
1663
+ var out = output;
1664
+ if (!out) {
1665
+ out = new Uint8Array(Math.ceil(hex.length / 2));
1666
+ }
1667
+ offset = offset || 0;
1668
+ var i = 0,
1669
+ j = offset;
1670
+ if (hex.length & 1) {
1671
+ // odd number of characters, convert first character alone
1672
+ i = 1;
1673
+ out[j++] = parseInt(hex[0], 16);
1674
+ }
1675
+ // convert 2 characters (1 byte) at a time
1676
+ for (; i < hex.length; i += 2) {
1677
+ out[j++] = parseInt(hex.substr(i, 2), 16);
1678
+ }
1679
+ return output ? j - offset : out;
1680
+ };
1681
+
1682
+ /**
1683
+ * Base64-encodes a Uint8Array.
1684
+ *
1685
+ * @param input the Uint8Array to encode.
1686
+ * @param maxline the maximum number of encoded characters per line to use,
1687
+ * defaults to none.
1688
+ *
1689
+ * @return the base64-encoded output string.
1690
+ */
1691
+ util$1.binary.base64.encode = function (input, maxline) {
1692
+ var line = '';
1693
+ var output = '';
1694
+ var chr1, chr2, chr3;
1695
+ var i = 0;
1696
+ while (i < input.byteLength) {
1697
+ chr1 = input[i++];
1698
+ chr2 = input[i++];
1699
+ chr3 = input[i++];
1700
+
1701
+ // encode 4 character group
1702
+ line += _base64.charAt(chr1 >> 2);
1703
+ line += _base64.charAt((chr1 & 3) << 4 | chr2 >> 4);
1704
+ if (isNaN(chr2)) {
1705
+ line += '==';
1706
+ } else {
1707
+ line += _base64.charAt((chr2 & 15) << 2 | chr3 >> 6);
1708
+ line += isNaN(chr3) ? '=' : _base64.charAt(chr3 & 63);
1709
+ }
1710
+ if (maxline && line.length > maxline) {
1711
+ output += line.substr(0, maxline) + '\r\n';
1712
+ line = line.substr(maxline);
1713
+ }
1714
+ }
1715
+ output += line;
1716
+ return output;
1717
+ };
1718
+
1719
+ /**
1720
+ * Decodes a base64-encoded string to a Uint8Array.
1721
+ *
1722
+ * @param input the base64-encoded input string.
1723
+ * @param [output] an optional Uint8Array to write the output to; if it
1724
+ * is too small, an exception will be thrown.
1725
+ * @param [offset] the start offset for writing to the output (default: 0).
1726
+ *
1727
+ * @return the Uint8Array or the number of bytes written if output was given.
1728
+ */
1729
+ util$1.binary.base64.decode = function (input, output, offset) {
1730
+ var out = output;
1731
+ if (!out) {
1732
+ out = new Uint8Array(Math.ceil(input.length / 4) * 3);
1733
+ }
1734
+
1735
+ // remove all non-base64 characters
1736
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
1737
+ offset = offset || 0;
1738
+ var enc1, enc2, enc3, enc4;
1739
+ var i = 0,
1740
+ j = offset;
1741
+ while (i < input.length) {
1742
+ enc1 = _base64Idx[input.charCodeAt(i++) - 43];
1743
+ enc2 = _base64Idx[input.charCodeAt(i++) - 43];
1744
+ enc3 = _base64Idx[input.charCodeAt(i++) - 43];
1745
+ enc4 = _base64Idx[input.charCodeAt(i++) - 43];
1746
+ out[j++] = enc1 << 2 | enc2 >> 4;
1747
+ if (enc3 !== 64) {
1748
+ // decoded at least 2 bytes
1749
+ out[j++] = (enc2 & 15) << 4 | enc3 >> 2;
1750
+ if (enc4 !== 64) {
1751
+ // decoded 3 bytes
1752
+ out[j++] = (enc3 & 3) << 6 | enc4;
1753
+ }
1754
+ }
1755
+ }
1756
+
1757
+ // make sure result is the exact decoded length
1758
+ return output ? j - offset : out.subarray(0, j);
1759
+ };
1760
+
1761
+ // add support for base58 encoding/decoding with Bitcoin alphabet
1762
+ util$1.binary.base58.encode = function (input, maxline) {
1763
+ return util$1.binary.baseN.encode(input, _base58, maxline);
1764
+ };
1765
+ util$1.binary.base58.decode = function (input, maxline) {
1766
+ return util$1.binary.baseN.decode(input, _base58, maxline);
1767
+ };
1768
+
1769
+ // text encoding/decoding tools
1770
+ // FIXME: Experimental. Do not use yet.
1771
+ util$1.text = {
1772
+ utf8: {},
1773
+ utf16: {}
1774
+ };
1775
+
1776
+ /**
1777
+ * Encodes the given string as UTF-8 in a Uint8Array.
1778
+ *
1779
+ * @param str the string to encode.
1780
+ * @param [output] an optional Uint8Array to write the output to; if it
1781
+ * is too small, an exception will be thrown.
1782
+ * @param [offset] the start offset for writing to the output (default: 0).
1783
+ *
1784
+ * @return the Uint8Array or the number of bytes written if output was given.
1785
+ */
1786
+ util$1.text.utf8.encode = function (str, output, offset) {
1787
+ str = util$1.encodeUtf8(str);
1788
+ var out = output;
1789
+ if (!out) {
1790
+ out = new Uint8Array(str.length);
1791
+ }
1792
+ offset = offset || 0;
1793
+ var j = offset;
1794
+ for (var i = 0; i < str.length; ++i) {
1795
+ out[j++] = str.charCodeAt(i);
1796
+ }
1797
+ return output ? j - offset : out;
1798
+ };
1799
+
1800
+ /**
1801
+ * Decodes the UTF-8 contents from a Uint8Array.
1802
+ *
1803
+ * @param bytes the Uint8Array to decode.
1804
+ *
1805
+ * @return the resulting string.
1806
+ */
1807
+ util$1.text.utf8.decode = function (bytes) {
1808
+ return util$1.decodeUtf8(String.fromCharCode.apply(null, bytes));
1809
+ };
1810
+
1811
+ /**
1812
+ * Encodes the given string as UTF-16 in a Uint8Array.
1813
+ *
1814
+ * @param str the string to encode.
1815
+ * @param [output] an optional Uint8Array to write the output to; if it
1816
+ * is too small, an exception will be thrown.
1817
+ * @param [offset] the start offset for writing to the output (default: 0).
1818
+ *
1819
+ * @return the Uint8Array or the number of bytes written if output was given.
1820
+ */
1821
+ util$1.text.utf16.encode = function (str, output, offset) {
1822
+ var out = output;
1823
+ if (!out) {
1824
+ out = new Uint8Array(str.length * 2);
1825
+ }
1826
+ var view = new Uint16Array(out.buffer);
1827
+ offset = offset || 0;
1828
+ var j = offset;
1829
+ var k = offset;
1830
+ for (var i = 0; i < str.length; ++i) {
1831
+ view[k++] = str.charCodeAt(i);
1832
+ j += 2;
1833
+ }
1834
+ return output ? j - offset : out;
1835
+ };
1836
+
1837
+ /**
1838
+ * Decodes the UTF-16 contents from a Uint8Array.
1839
+ *
1840
+ * @param bytes the Uint8Array to decode.
1841
+ *
1842
+ * @return the resulting string.
1843
+ */
1844
+ util$1.text.utf16.decode = function (bytes) {
1845
+ return String.fromCharCode.apply(null, new Uint16Array(bytes.buffer));
1846
+ };
1847
+
1848
+ /**
1849
+ * Deflates the given data using a flash interface.
1850
+ *
1851
+ * @param api the flash interface.
1852
+ * @param bytes the data.
1853
+ * @param raw true to return only raw deflate data, false to include zlib
1854
+ * header and trailer.
1855
+ *
1856
+ * @return the deflated data as a string.
1857
+ */
1858
+ util$1.deflate = function (api, bytes, raw) {
1859
+ bytes = util$1.decode64(api.deflate(util$1.encode64(bytes)).rval);
1860
+
1861
+ // strip zlib header and trailer if necessary
1862
+ if (raw) {
1863
+ // zlib header is 2 bytes (CMF,FLG) where FLG indicates that
1864
+ // there is a 4-byte DICT (alder-32) block before the data if
1865
+ // its 5th bit is set
1866
+ var start = 2;
1867
+ var flg = bytes.charCodeAt(1);
1868
+ if (flg & 0x20) {
1869
+ start = 6;
1870
+ }
1871
+ // zlib trailer is 4 bytes of adler-32
1872
+ bytes = bytes.substring(start, bytes.length - 4);
1873
+ }
1874
+ return bytes;
1875
+ };
1876
+
1877
+ /**
1878
+ * Inflates the given data using a flash interface.
1879
+ *
1880
+ * @param api the flash interface.
1881
+ * @param bytes the data.
1882
+ * @param raw true if the incoming data has no zlib header or trailer and is
1883
+ * raw DEFLATE data.
1884
+ *
1885
+ * @return the inflated data as a string, null on error.
1886
+ */
1887
+ util$1.inflate = function (api, bytes, raw) {
1888
+ // TODO: add zlib header and trailer if necessary/possible
1889
+ var rval = api.inflate(util$1.encode64(bytes)).rval;
1890
+ return rval === null ? null : util$1.decode64(rval);
1891
+ };
1892
+
1893
+ /**
1894
+ * Sets a storage object.
1895
+ *
1896
+ * @param api the storage interface.
1897
+ * @param id the storage ID to use.
1898
+ * @param obj the storage object, null to remove.
1899
+ */
1900
+ var _setStorageObject = function (api, id, obj) {
1901
+ if (!api) {
1902
+ throw new Error('WebStorage not available.');
1903
+ }
1904
+ var rval;
1905
+ if (obj === null) {
1906
+ rval = api.removeItem(id);
1907
+ } else {
1908
+ // json-encode and base64-encode object
1909
+ obj = util$1.encode64(JSON.stringify(obj));
1910
+ rval = api.setItem(id, obj);
1911
+ }
1912
+
1913
+ // handle potential flash error
1914
+ if (typeof rval !== 'undefined' && rval.rval !== true) {
1915
+ var error = new Error(rval.error.message);
1916
+ error.id = rval.error.id;
1917
+ error.name = rval.error.name;
1918
+ throw error;
1919
+ }
1920
+ };
1921
+
1922
+ /**
1923
+ * Gets a storage object.
1924
+ *
1925
+ * @param api the storage interface.
1926
+ * @param id the storage ID to use.
1927
+ *
1928
+ * @return the storage object entry or null if none exists.
1929
+ */
1930
+ var _getStorageObject = function (api, id) {
1931
+ if (!api) {
1932
+ throw new Error('WebStorage not available.');
1933
+ }
1934
+
1935
+ // get the existing entry
1936
+ var rval = api.getItem(id);
1937
+
1938
+ /* Note: We check api.init because we can't do (api == localStorage)
1939
+ on IE because of "Class doesn't support Automation" exception. Only
1940
+ the flash api has an init method so this works too, but we need a
1941
+ better solution in the future. */
1942
+
1943
+ // flash returns item wrapped in an object, handle special case
1944
+ if (api.init) {
1945
+ if (rval.rval === null) {
1946
+ if (rval.error) {
1947
+ var error = new Error(rval.error.message);
1948
+ error.id = rval.error.id;
1949
+ error.name = rval.error.name;
1950
+ throw error;
1951
+ }
1952
+ // no error, but also no item
1953
+ rval = null;
1954
+ } else {
1955
+ rval = rval.rval;
1956
+ }
1957
+ }
1958
+
1959
+ // handle decoding
1960
+ if (rval !== null) {
1961
+ // base64-decode and json-decode data
1962
+ rval = JSON.parse(util$1.decode64(rval));
1963
+ }
1964
+ return rval;
1965
+ };
1966
+
1967
+ /**
1968
+ * Stores an item in local storage.
1969
+ *
1970
+ * @param api the storage interface.
1971
+ * @param id the storage ID to use.
1972
+ * @param key the key for the item.
1973
+ * @param data the data for the item (any javascript object/primitive).
1974
+ */
1975
+ var _setItem = function (api, id, key, data) {
1976
+ // get storage object
1977
+ var obj = _getStorageObject(api, id);
1978
+ if (obj === null) {
1979
+ // create a new storage object
1980
+ obj = {};
1981
+ }
1982
+ // update key
1983
+ obj[key] = data;
1984
+
1985
+ // set storage object
1986
+ _setStorageObject(api, id, obj);
1987
+ };
1988
+
1989
+ /**
1990
+ * Gets an item from local storage.
1991
+ *
1992
+ * @param api the storage interface.
1993
+ * @param id the storage ID to use.
1994
+ * @param key the key for the item.
1995
+ *
1996
+ * @return the item.
1997
+ */
1998
+ var _getItem = function (api, id, key) {
1999
+ // get storage object
2000
+ var rval = _getStorageObject(api, id);
2001
+ if (rval !== null) {
2002
+ // return data at key
2003
+ rval = key in rval ? rval[key] : null;
2004
+ }
2005
+ return rval;
2006
+ };
2007
+
2008
+ /**
2009
+ * Removes an item from local storage.
2010
+ *
2011
+ * @param api the storage interface.
2012
+ * @param id the storage ID to use.
2013
+ * @param key the key for the item.
2014
+ */
2015
+ var _removeItem = function (api, id, key) {
2016
+ // get storage object
2017
+ var obj = _getStorageObject(api, id);
2018
+ if (obj !== null && key in obj) {
2019
+ // remove key
2020
+ delete obj[key];
2021
+
2022
+ // see if entry has no keys remaining
2023
+ var empty = true;
2024
+ for (var prop in obj) {
2025
+ empty = false;
2026
+ break;
2027
+ }
2028
+ if (empty) {
2029
+ // remove entry entirely if no keys are left
2030
+ obj = null;
2031
+ }
2032
+
2033
+ // set storage object
2034
+ _setStorageObject(api, id, obj);
2035
+ }
2036
+ };
2037
+
2038
+ /**
2039
+ * Clears the local disk storage identified by the given ID.
2040
+ *
2041
+ * @param api the storage interface.
2042
+ * @param id the storage ID to use.
2043
+ */
2044
+ var _clearItems = function (api, id) {
2045
+ _setStorageObject(api, id, null);
2046
+ };
2047
+
2048
+ /**
2049
+ * Calls a storage function.
2050
+ *
2051
+ * @param func the function to call.
2052
+ * @param args the arguments for the function.
2053
+ * @param location the location argument.
2054
+ *
2055
+ * @return the return value from the function.
2056
+ */
2057
+ var _callStorageFunction = function (func, args, location) {
2058
+ var rval = null;
2059
+
2060
+ // default storage types
2061
+ if (typeof location === 'undefined') {
2062
+ location = ['web', 'flash'];
2063
+ }
2064
+
2065
+ // apply storage types in order of preference
2066
+ var type;
2067
+ var done = false;
2068
+ var exception = null;
2069
+ for (var idx in location) {
2070
+ type = location[idx];
2071
+ try {
2072
+ if (type === 'flash' || type === 'both') {
2073
+ if (args[0] === null) {
2074
+ throw new Error('Flash local storage not available.');
2075
+ }
2076
+ rval = func.apply(this, args);
2077
+ done = type === 'flash';
2078
+ }
2079
+ if (type === 'web' || type === 'both') {
2080
+ args[0] = localStorage;
2081
+ rval = func.apply(this, args);
2082
+ done = true;
2083
+ }
2084
+ } catch (ex) {
2085
+ exception = ex;
2086
+ }
2087
+ if (done) {
2088
+ break;
2089
+ }
2090
+ }
2091
+ if (!done) {
2092
+ throw exception;
2093
+ }
2094
+ return rval;
2095
+ };
2096
+
2097
+ /**
2098
+ * Stores an item on local disk.
2099
+ *
2100
+ * The available types of local storage include 'flash', 'web', and 'both'.
2101
+ *
2102
+ * The type 'flash' refers to flash local storage (SharedObject). In order
2103
+ * to use flash local storage, the 'api' parameter must be valid. The type
2104
+ * 'web' refers to WebStorage, if supported by the browser. The type 'both'
2105
+ * refers to storing using both 'flash' and 'web', not just one or the
2106
+ * other.
2107
+ *
2108
+ * The location array should list the storage types to use in order of
2109
+ * preference:
2110
+ *
2111
+ * ['flash']: flash only storage
2112
+ * ['web']: web only storage
2113
+ * ['both']: try to store in both
2114
+ * ['flash','web']: store in flash first, but if not available, 'web'
2115
+ * ['web','flash']: store in web first, but if not available, 'flash'
2116
+ *
2117
+ * The location array defaults to: ['web', 'flash']
2118
+ *
2119
+ * @param api the flash interface, null to use only WebStorage.
2120
+ * @param id the storage ID to use.
2121
+ * @param key the key for the item.
2122
+ * @param data the data for the item (any javascript object/primitive).
2123
+ * @param location an array with the preferred types of storage to use.
2124
+ */
2125
+ util$1.setItem = function (api, id, key, data, location) {
2126
+ _callStorageFunction(_setItem, arguments, location);
2127
+ };
2128
+
2129
+ /**
2130
+ * Gets an item on local disk.
2131
+ *
2132
+ * Set setItem() for details on storage types.
2133
+ *
2134
+ * @param api the flash interface, null to use only WebStorage.
2135
+ * @param id the storage ID to use.
2136
+ * @param key the key for the item.
2137
+ * @param location an array with the preferred types of storage to use.
2138
+ *
2139
+ * @return the item.
2140
+ */
2141
+ util$1.getItem = function (api, id, key, location) {
2142
+ return _callStorageFunction(_getItem, arguments, location);
2143
+ };
2144
+
2145
+ /**
2146
+ * Removes an item on local disk.
2147
+ *
2148
+ * Set setItem() for details on storage types.
2149
+ *
2150
+ * @param api the flash interface.
2151
+ * @param id the storage ID to use.
2152
+ * @param key the key for the item.
2153
+ * @param location an array with the preferred types of storage to use.
2154
+ */
2155
+ util$1.removeItem = function (api, id, key, location) {
2156
+ _callStorageFunction(_removeItem, arguments, location);
2157
+ };
2158
+
2159
+ /**
2160
+ * Clears the local disk storage identified by the given ID.
2161
+ *
2162
+ * Set setItem() for details on storage types.
2163
+ *
2164
+ * @param api the flash interface if flash is available.
2165
+ * @param id the storage ID to use.
2166
+ * @param location an array with the preferred types of storage to use.
2167
+ */
2168
+ util$1.clearItems = function (api, id, location) {
2169
+ _callStorageFunction(_clearItems, arguments, location);
2170
+ };
2171
+
2172
+ /**
2173
+ * Check if an object is empty.
2174
+ *
2175
+ * Taken from:
2176
+ * http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object-from-json/679937#679937
2177
+ *
2178
+ * @param object the object to check.
2179
+ */
2180
+ util$1.isEmpty = function (obj) {
2181
+ for (var prop in obj) {
2182
+ if (obj.hasOwnProperty(prop)) {
2183
+ return false;
2184
+ }
2185
+ }
2186
+ return true;
2187
+ };
2188
+
2189
+ /**
2190
+ * Format with simple printf-style interpolation.
2191
+ *
2192
+ * %%: literal '%'
2193
+ * %s,%o: convert next argument into a string.
2194
+ *
2195
+ * @param format the string to format.
2196
+ * @param ... arguments to interpolate into the format string.
2197
+ */
2198
+ util$1.format = function (format) {
2199
+ var re = /%./g;
2200
+ // current match
2201
+ var match;
2202
+ // current part
2203
+ var part;
2204
+ // current arg index
2205
+ var argi = 0;
2206
+ // collected parts to recombine later
2207
+ var parts = [];
2208
+ // last index found
2209
+ var last = 0;
2210
+ // loop while matches remain
2211
+ while (match = re.exec(format)) {
2212
+ part = format.substring(last, re.lastIndex - 2);
2213
+ // don't add empty strings (ie, parts between %s%s)
2214
+ if (part.length > 0) {
2215
+ parts.push(part);
2216
+ }
2217
+ last = re.lastIndex;
2218
+ // switch on % code
2219
+ var code = match[0][1];
2220
+ switch (code) {
2221
+ case 's':
2222
+ case 'o':
2223
+ // check if enough arguments were given
2224
+ if (argi < arguments.length) {
2225
+ parts.push(arguments[argi++ + 1]);
2226
+ } else {
2227
+ parts.push('<?>');
2228
+ }
2229
+ break;
2230
+ // FIXME: do proper formating for numbers, etc
2231
+ //case 'f':
2232
+ //case 'd':
2233
+ case '%':
2234
+ parts.push('%');
2235
+ break;
2236
+ default:
2237
+ parts.push('<%' + code + '?>');
2238
+ }
2239
+ }
2240
+ // add trailing part of format string
2241
+ parts.push(format.substring(last));
2242
+ return parts.join('');
2243
+ };
2244
+
2245
+ /**
2246
+ * Formats a number.
2247
+ *
2248
+ * http://snipplr.com/view/5945/javascript-numberformat--ported-from-php/
2249
+ */
2250
+ util$1.formatNumber = function (number, decimals, dec_point, thousands_sep) {
2251
+ // http://kevin.vanzonneveld.net
2252
+ // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
2253
+ // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
2254
+ // + bugfix by: Michael White (http://crestidg.com)
2255
+ // + bugfix by: Benjamin Lupton
2256
+ // + bugfix by: Allan Jensen (http://www.winternet.no)
2257
+ // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
2258
+ // * example 1: number_format(1234.5678, 2, '.', '');
2259
+ // * returns 1: 1234.57
2260
+
2261
+ var n = number,
2262
+ c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
2263
+ var d = dec_point === undefined ? ',' : dec_point;
2264
+ var t = thousands_sep === undefined ? '.' : thousands_sep,
2265
+ s = n < 0 ? '-' : '';
2266
+ var i = parseInt(n = Math.abs(+n || 0).toFixed(c), 10) + '';
2267
+ var j = i.length > 3 ? i.length % 3 : 0;
2268
+ return s + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : '');
2269
+ };
2270
+
2271
+ /**
2272
+ * Formats a byte size.
2273
+ *
2274
+ * http://snipplr.com/view/5949/format-humanize-file-byte-size-presentation-in-javascript/
2275
+ */
2276
+ util$1.formatSize = function (size) {
2277
+ if (size >= 1073741824) {
2278
+ size = util$1.formatNumber(size / 1073741824, 2, '.', '') + ' GiB';
2279
+ } else if (size >= 1048576) {
2280
+ size = util$1.formatNumber(size / 1048576, 2, '.', '') + ' MiB';
2281
+ } else if (size >= 1024) {
2282
+ size = util$1.formatNumber(size / 1024, 0) + ' KiB';
2283
+ } else {
2284
+ size = util$1.formatNumber(size, 0) + ' bytes';
2285
+ }
2286
+ return size;
2287
+ };
2288
+
2289
+ /**
2290
+ * Converts an IPv4 or IPv6 string representation into bytes (in network order).
2291
+ *
2292
+ * @param ip the IPv4 or IPv6 address to convert.
2293
+ *
2294
+ * @return the 4-byte IPv6 or 16-byte IPv6 address or null if the address can't
2295
+ * be parsed.
2296
+ */
2297
+ util$1.bytesFromIP = function (ip) {
2298
+ if (ip.indexOf('.') !== -1) {
2299
+ return util$1.bytesFromIPv4(ip);
2300
+ }
2301
+ if (ip.indexOf(':') !== -1) {
2302
+ return util$1.bytesFromIPv6(ip);
2303
+ }
2304
+ return null;
2305
+ };
2306
+
2307
+ /**
2308
+ * Converts an IPv4 string representation into bytes (in network order).
2309
+ *
2310
+ * @param ip the IPv4 address to convert.
2311
+ *
2312
+ * @return the 4-byte address or null if the address can't be parsed.
2313
+ */
2314
+ util$1.bytesFromIPv4 = function (ip) {
2315
+ ip = ip.split('.');
2316
+ if (ip.length !== 4) {
2317
+ return null;
2318
+ }
2319
+ var b = util$1.createBuffer();
2320
+ for (var i = 0; i < ip.length; ++i) {
2321
+ var num = parseInt(ip[i], 10);
2322
+ if (isNaN(num)) {
2323
+ return null;
2324
+ }
2325
+ b.putByte(num);
2326
+ }
2327
+ return b.getBytes();
2328
+ };
2329
+
2330
+ /**
2331
+ * Converts an IPv6 string representation into bytes (in network order).
2332
+ *
2333
+ * @param ip the IPv6 address to convert.
2334
+ *
2335
+ * @return the 16-byte address or null if the address can't be parsed.
2336
+ */
2337
+ util$1.bytesFromIPv6 = function (ip) {
2338
+ var blanks = 0;
2339
+ ip = ip.split(':').filter(function (e) {
2340
+ if (e.length === 0) ++blanks;
2341
+ return true;
2342
+ });
2343
+ var zeros = (8 - ip.length + blanks) * 2;
2344
+ var b = util$1.createBuffer();
2345
+ for (var i = 0; i < 8; ++i) {
2346
+ if (!ip[i] || ip[i].length === 0) {
2347
+ b.fillWithByte(0, zeros);
2348
+ zeros = 0;
2349
+ continue;
2350
+ }
2351
+ var bytes = util$1.hexToBytes(ip[i]);
2352
+ if (bytes.length < 2) {
2353
+ b.putByte(0);
2354
+ }
2355
+ b.putBytes(bytes);
2356
+ }
2357
+ return b.getBytes();
2358
+ };
2359
+
2360
+ /**
2361
+ * Converts 4-bytes into an IPv4 string representation or 16-bytes into
2362
+ * an IPv6 string representation. The bytes must be in network order.
2363
+ *
2364
+ * @param bytes the bytes to convert.
2365
+ *
2366
+ * @return the IPv4 or IPv6 string representation if 4 or 16 bytes,
2367
+ * respectively, are given, otherwise null.
2368
+ */
2369
+ util$1.bytesToIP = function (bytes) {
2370
+ if (bytes.length === 4) {
2371
+ return util$1.bytesToIPv4(bytes);
2372
+ }
2373
+ if (bytes.length === 16) {
2374
+ return util$1.bytesToIPv6(bytes);
2375
+ }
2376
+ return null;
2377
+ };
2378
+
2379
+ /**
2380
+ * Converts 4-bytes into an IPv4 string representation. The bytes must be
2381
+ * in network order.
2382
+ *
2383
+ * @param bytes the bytes to convert.
2384
+ *
2385
+ * @return the IPv4 string representation or null for an invalid # of bytes.
2386
+ */
2387
+ util$1.bytesToIPv4 = function (bytes) {
2388
+ if (bytes.length !== 4) {
2389
+ return null;
2390
+ }
2391
+ var ip = [];
2392
+ for (var i = 0; i < bytes.length; ++i) {
2393
+ ip.push(bytes.charCodeAt(i));
2394
+ }
2395
+ return ip.join('.');
2396
+ };
2397
+
2398
+ /**
2399
+ * Converts 16-bytes into an IPv16 string representation. The bytes must be
2400
+ * in network order.
2401
+ *
2402
+ * @param bytes the bytes to convert.
2403
+ *
2404
+ * @return the IPv16 string representation or null for an invalid # of bytes.
2405
+ */
2406
+ util$1.bytesToIPv6 = function (bytes) {
2407
+ if (bytes.length !== 16) {
2408
+ return null;
2409
+ }
2410
+ var ip = [];
2411
+ var zeroGroups = [];
2412
+ var zeroMaxGroup = 0;
2413
+ for (var i = 0; i < bytes.length; i += 2) {
2414
+ var hex = util$1.bytesToHex(bytes[i] + bytes[i + 1]);
2415
+ // canonicalize zero representation
2416
+ while (hex[0] === '0' && hex !== '0') {
2417
+ hex = hex.substr(1);
2418
+ }
2419
+ if (hex === '0') {
2420
+ var last = zeroGroups[zeroGroups.length - 1];
2421
+ var idx = ip.length;
2422
+ if (!last || idx !== last.end + 1) {
2423
+ zeroGroups.push({
2424
+ start: idx,
2425
+ end: idx
2426
+ });
2427
+ } else {
2428
+ last.end = idx;
2429
+ if (last.end - last.start > zeroGroups[zeroMaxGroup].end - zeroGroups[zeroMaxGroup].start) {
2430
+ zeroMaxGroup = zeroGroups.length - 1;
2431
+ }
2432
+ }
2433
+ }
2434
+ ip.push(hex);
2435
+ }
2436
+ if (zeroGroups.length > 0) {
2437
+ var group = zeroGroups[zeroMaxGroup];
2438
+ // only shorten group of length > 0
2439
+ if (group.end - group.start > 0) {
2440
+ ip.splice(group.start, group.end - group.start + 1, '');
2441
+ if (group.start === 0) {
2442
+ ip.unshift('');
2443
+ }
2444
+ if (group.end === 7) {
2445
+ ip.push('');
2446
+ }
2447
+ }
2448
+ }
2449
+ return ip.join(':');
2450
+ };
2451
+
2452
+ /**
2453
+ * Estimates the number of processes that can be run concurrently. If
2454
+ * creating Web Workers, keep in mind that the main JavaScript process needs
2455
+ * its own core.
2456
+ *
2457
+ * @param options the options to use:
2458
+ * update true to force an update (not use the cached value).
2459
+ * @param callback(err, max) called once the operation completes.
2460
+ */
2461
+ util$1.estimateCores = function (options, callback) {
2462
+ if (typeof options === 'function') {
2463
+ callback = options;
2464
+ options = {};
2465
+ }
2466
+ options = options || {};
2467
+ if ('cores' in util$1 && !options.update) {
2468
+ return callback(null, util$1.cores);
2469
+ }
2470
+ if (typeof navigator !== 'undefined' && 'hardwareConcurrency' in navigator && navigator.hardwareConcurrency > 0) {
2471
+ util$1.cores = navigator.hardwareConcurrency;
2472
+ return callback(null, util$1.cores);
2473
+ }
2474
+ if (typeof Worker === 'undefined') {
2475
+ // workers not available
2476
+ util$1.cores = 1;
2477
+ return callback(null, util$1.cores);
2478
+ }
2479
+ if (typeof Blob === 'undefined') {
2480
+ // can't estimate, default to 2
2481
+ util$1.cores = 2;
2482
+ return callback(null, util$1.cores);
2483
+ }
2484
+
2485
+ // create worker concurrency estimation code as blob
2486
+ var blobUrl = URL.createObjectURL(new Blob(['(', function () {
2487
+ self.addEventListener('message', function (e) {
2488
+ // run worker for 4 ms
2489
+ var st = Date.now();
2490
+ var et = st + 4;
2491
+ self.postMessage({
2492
+ st: st,
2493
+ et: et
2494
+ });
2495
+ });
2496
+ }.toString(), ')()'], {
2497
+ type: 'application/javascript'
2498
+ }));
2499
+
2500
+ // take 5 samples using 16 workers
2501
+ sample([], 5, 16);
2502
+ function sample(max, samples, numWorkers) {
2503
+ if (samples === 0) {
2504
+ // get overlap average
2505
+ var avg = Math.floor(max.reduce(function (avg, x) {
2506
+ return avg + x;
2507
+ }, 0) / max.length);
2508
+ util$1.cores = Math.max(1, avg);
2509
+ URL.revokeObjectURL(blobUrl);
2510
+ return callback(null, util$1.cores);
2511
+ }
2512
+ map(numWorkers, function (err, results) {
2513
+ max.push(reduce(numWorkers, results));
2514
+ sample(max, samples - 1, numWorkers);
2515
+ });
2516
+ }
2517
+ function map(numWorkers, callback) {
2518
+ var workers = [];
2519
+ var results = [];
2520
+ for (var i = 0; i < numWorkers; ++i) {
2521
+ var worker = new Worker(blobUrl);
2522
+ worker.addEventListener('message', function (e) {
2523
+ results.push(e.data);
2524
+ if (results.length === numWorkers) {
2525
+ for (var i = 0; i < numWorkers; ++i) {
2526
+ workers[i].terminate();
2527
+ }
2528
+ callback(null, results);
2529
+ }
2530
+ });
2531
+ workers.push(worker);
2532
+ }
2533
+ for (var i = 0; i < numWorkers; ++i) {
2534
+ workers[i].postMessage(i);
2535
+ }
2536
+ }
2537
+ function reduce(numWorkers, results) {
2538
+ // find overlapping time windows
2539
+ var overlaps = [];
2540
+ for (var n = 0; n < numWorkers; ++n) {
2541
+ var r1 = results[n];
2542
+ var overlap = overlaps[n] = [];
2543
+ for (var i = 0; i < numWorkers; ++i) {
2544
+ if (n === i) {
2545
+ continue;
2546
+ }
2547
+ var r2 = results[i];
2548
+ if (r1.st > r2.st && r1.st < r2.et || r2.st > r1.st && r2.st < r1.et) {
2549
+ overlap.push(i);
2550
+ }
2551
+ }
2552
+ }
2553
+ // get maximum overlaps ... don't include overlapping worker itself
2554
+ // as the main JS process was also being scheduled during the work and
2555
+ // would have to be subtracted from the estimate anyway
2556
+ return overlaps.reduce(function (max, overlap) {
2557
+ return Math.max(max, overlap.length);
2558
+ }, 0);
2559
+ }
2560
+ };
2561
+ return util.exports;
2562
+ }
2563
+
2564
+ export { requireUtil as __require };
2565
+ //# sourceMappingURL=util.js.map