├── .gitignore ├── test ├── mocha.opts ├── test.html └── ipfs.js ├── example.url ├── package.json ├── dist └── ipfs.min.js ├── README.md ├── ipfs.js └── example ├── index.html ├── file-type.js └── buffer.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --timeout 10000 2 | -------------------------------------------------------------------------------- /example.url: -------------------------------------------------------------------------------- 1 | { ipfs config Addresses.Gateway | awk -F'/' '{ printf "http://" $3 ":" $5 }'; ipfs add -r example | tail -n 1 | awk '{ printf "/ipfs/" $2 }';} | xargs echo 2 | -------------------------------------------------------------------------------- /test/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Mocha Tests 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 21 | 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "browser-ipfs", 3 | "version": "0.0.6", 4 | "description": "Simplified IPFS api targeted at the browser", 5 | "main": "ipfs.js", 6 | "scripts": { 7 | "build": "./node_modules/.bin/uglifyjs ./ipfs.js -m > dist/ipfs.min.js", 8 | "test": "./node_modules/.bin/mocha" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/pelle/browser-ipfs.git" 13 | }, 14 | "homepage": "https://github.com/pelle/browser-ipfs", 15 | "bugs": { 16 | "url": "https://github.com/pelle/browser-ipfs/issues" 17 | }, 18 | "keywords": [ 19 | "ipfs" 20 | ], 21 | "author": "ConsenSys", 22 | "license": "ISC", 23 | "devDependencies": { 24 | "uglify-js": "^2.4.24" 25 | }, 26 | "dependencies": {}, 27 | "authors": [ 28 | "Jeff Scott Ward", 29 | "Christian Lundkvist", 30 | "Connor Keenan", 31 | "Pelle Braendgaard" 32 | ] 33 | } 34 | -------------------------------------------------------------------------------- /dist/ipfs.min.js: -------------------------------------------------------------------------------- 1 | (function(){var t={};t.localProvider={host:"127.0.0.1",port:"5001",protocol:"http",root:"/api/v0"};t.setProvider=function(o){if(!o)o=this.localProvider;if(typeof o==="object"&&!o.hasOwnProperty("host")){return}t.api=o};t.api_url=function(o){var r=t.api;return r.protocol+"://"+r.host+(r.port?":"+r.port:"")+(r.root?r.root:"")+o};function o(o){if(!t.api){o("No provider set",null);return false}return true}function r(r){if(!o(r.callback))return;var e=new XMLHttpRequest;e.onreadystatechange=function(){if(e.readyState==4){if(e.status!=200)r.callback(e.responseText,null);else{var t=e.responseText;if(r.transform){t=r.transform(t)}r.callback(null,t)}}};e.open(r.method||"GET",t.api_url(r.uri));if(r.accept){e.setRequestHeader("accept",r.accept)}if(r.payload){e.enctype="multipart/form-data";e.send(r.payload)}else{e.send()}}t.add=function(t,o){var n=new FormData;var a=e(t)?t.toString("binary"):t;var i=new Blob([a],{});n.append("file",i);r({callback:o,method:"POST",uri:"/add",payload:n,accept:"application/json",transform:function(t){return t?JSON.parse(t)["Hash"]:null}})};t.catText=function(t,o){r({callback:o,uri:"/cat/"+t})};t.cat=t.catText;t.addJson=function(o,r){var e=JSON.stringify(o);t.add(e,r)};t.catJson=function(o,r){t.catText(o,function(t,o){if(t)r(t,{});var e={};try{e=typeof o==="string"?JSON.parse(o):o}catch(n){t=n}r(t,e)})};function e(t){return!!(t!=null&&(t._isBuffer||t.constructor&&typeof t.constructor.isBuffer==="function"&&t.constructor.isBuffer(t)))}if(typeof window!=="undefined"){window.ipfs=t}if(typeof module!=="undefined"&&module.exports){module.exports=t}})(); 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Browser friendly ipfs.js light 2 | 3 | This is meant to implement the most common use cases for using ipfs in a browser. It has zero dependencies. 4 | 5 | It meant to be mostly compatible with [ipfs.js](https://github.com/consensys/ipfs.js) with a few key differences: 6 | 7 | - no specific support for Buffers. Buffer's still work, see caveat below in documentation for ipfs.cat 8 | - only implements simple add and cat functionality 9 | - Basically if you're using ipfs as a simple KV store, this should do the trick 10 | 11 | Run `npm install browser-ipfs` 12 | 13 | Or reference `dist/ipfs.min.js` inside a ` 15 | 16 | 17 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /example/file-type.js: -------------------------------------------------------------------------------- 1 | // from https://github.com/sindresorhus/file-type 2 | 'use strict'; 3 | window.fileType = function (buf) { 4 | if (!(buf && buf.length > 1)) { 5 | return null; 6 | } 7 | 8 | if (buf[0] === 0xFF && buf[1] === 0xD8 && buf[2] === 0xFF) { 9 | return { 10 | ext: 'jpg', 11 | mime: 'image/jpeg' 12 | }; 13 | } 14 | 15 | if (buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4E && buf[3] === 0x47) { 16 | return { 17 | ext: 'png', 18 | mime: 'image/png' 19 | }; 20 | } 21 | 22 | if (buf[0] === 0x47 && buf[1] === 0x49 && buf[2] === 0x46) { 23 | return { 24 | ext: 'gif', 25 | mime: 'image/gif' 26 | }; 27 | } 28 | 29 | if (buf[8] === 0x57 && buf[9] === 0x45 && buf[10] === 0x42 && buf[11] === 0x50) { 30 | return { 31 | ext: 'webp', 32 | mime: 'image/webp' 33 | }; 34 | } 35 | 36 | // needs to be before `tif` check 37 | if (((buf[0] === 0x49 && buf[1] === 0x49 && buf[2] === 0x2A && buf[3] === 0x0) || (buf[0] === 0x4D && buf[1] === 0x4D && buf[2] === 0x0 && buf[3] === 0x2A)) && buf[8] === 0x43 && buf[9] === 0x52) { 38 | return { 39 | ext: 'cr2', 40 | mime: 'image/x-canon-cr2' 41 | }; 42 | } 43 | 44 | if ((buf[0] === 0x49 && buf[1] === 0x49 && buf[2] === 0x2A && buf[3] === 0x0) || (buf[0] === 0x4D && buf[1] === 0x4D && buf[2] === 0x0 && buf[3] === 0x2A)) { 45 | return { 46 | ext: 'tif', 47 | mime: 'image/tiff' 48 | }; 49 | } 50 | 51 | if (buf[0] === 0x42 && buf[1] === 0x4D) { 52 | return { 53 | ext: 'bmp', 54 | mime: 'image/bmp' 55 | }; 56 | } 57 | 58 | if (buf[0] === 0x49 && buf[1] === 0x49 && buf[2] === 0xBC) { 59 | return { 60 | ext: 'jxr', 61 | mime: 'image/vnd.ms-photo' 62 | }; 63 | } 64 | 65 | if (buf[0] === 0x38 && buf[1] === 0x42 && buf[2] === 0x50 && buf[3] === 0x53) { 66 | return { 67 | ext: 'psd', 68 | mime: 'image/vnd.adobe.photoshop' 69 | }; 70 | } 71 | 72 | // needs to be before `zip` check 73 | if (buf[0] === 0x50 && buf[1] === 0x4B && buf[2] === 0x3 && buf[3] === 0x4 && buf[30] === 0x6D && buf[31] === 0x69 && buf[32] === 0x6D && buf[33] === 0x65 && buf[34] === 0x74 && buf[35] === 0x79 && buf[36] === 0x70 && buf[37] === 0x65 && buf[38] === 0x61 && buf[39] === 0x70 && buf[40] === 0x70 && buf[41] === 0x6C && buf[42] === 0x69 && buf[43] === 0x63 && buf[44] === 0x61 && buf[45] === 0x74 && buf[46] === 0x69 && buf[47] === 0x6F && buf[48] === 0x6E && buf[49] === 0x2F && buf[50] === 0x65 && buf[51] === 0x70 && buf[52] === 0x75 && buf[53] === 0x62 && buf[54] === 0x2B && buf[55] === 0x7A && buf[56] === 0x69 && buf[57] === 0x70) { 74 | return { 75 | ext: 'epub', 76 | mime: 'application/epub+zip' 77 | }; 78 | } 79 | 80 | // needs to be before `zip` check 81 | // assumes signed .xpi from addons.mozilla.org 82 | if (buf[0] === 0x50 && buf[1] === 0x4B && buf[2] === 0x3 && buf[3] === 0x4 && buf[30] === 0x4D && buf[31] === 0x45 && buf[32] === 0x54 && buf[33] === 0x41 && buf[34] === 0x2D && buf[35] === 0x49 && buf[36] === 0x4E && buf[37] === 0x46 && buf[38] === 0x2F && buf[39] === 0x6D && buf[40] === 0x6F && buf[41] === 0x7A && buf[42] === 0x69 && buf[43] === 0x6C && buf[44] === 0x6C && buf[45] === 0x61 && buf[46] === 0x2E && buf[47] === 0x72 && buf[48] === 0x73 && buf[49] === 0x61) { 83 | return { 84 | ext: 'xpi', 85 | mime: 'application/x-xpinstall' 86 | }; 87 | } 88 | 89 | if (buf[0] === 0x50 && buf[1] === 0x4B && (buf[2] === 0x3 || buf[2] === 0x5 || buf[2] === 0x7) && (buf[3] === 0x4 || buf[3] === 0x6 || buf[3] === 0x8)) { 90 | return { 91 | ext: 'zip', 92 | mime: 'application/zip' 93 | }; 94 | } 95 | 96 | if (buf[257] === 0x75 && buf[258] === 0x73 && buf[259] === 0x74 && buf[260] === 0x61 && buf[261] === 0x72) { 97 | return { 98 | ext: 'tar', 99 | mime: 'application/x-tar' 100 | }; 101 | } 102 | 103 | if (buf[0] === 0x52 && buf[1] === 0x61 && buf[2] === 0x72 && buf[3] === 0x21 && buf[4] === 0x1A && buf[5] === 0x7 && (buf[6] === 0x0 || buf[6] === 0x1)) { 104 | return { 105 | ext: 'rar', 106 | mime: 'application/x-rar-compressed' 107 | }; 108 | } 109 | 110 | if (buf[0] === 0x1F && buf[1] === 0x8B && buf[2] === 0x8) { 111 | return { 112 | ext: 'gz', 113 | mime: 'application/gzip' 114 | }; 115 | } 116 | 117 | if (buf[0] === 0x42 && buf[1] === 0x5A && buf[2] === 0x68) { 118 | return { 119 | ext: 'bz2', 120 | mime: 'application/x-bzip2' 121 | }; 122 | } 123 | 124 | if (buf[0] === 0x37 && buf[1] === 0x7A && buf[2] === 0xBC && buf[3] === 0xAF && buf[4] === 0x27 && buf[5] === 0x1C) { 125 | return { 126 | ext: '7z', 127 | mime: 'application/x-7z-compressed' 128 | }; 129 | } 130 | 131 | if (buf[0] === 0x78 && buf[1] === 0x01) { 132 | return { 133 | ext: 'dmg', 134 | mime: 'application/x-apple-diskimage' 135 | }; 136 | } 137 | 138 | if ( 139 | (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && (buf[3] === 0x18 || buf[3] === 0x20) && buf[4] === 0x66 && buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70) || 140 | (buf[0] === 0x33 && buf[1] === 0x67 && buf[2] === 0x70 && buf[3] === 0x35) || 141 | (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1C && buf[4] === 0x66 && buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x6D && buf[9] === 0x70 && buf[10] === 0x34 && buf[11] === 0x32 && buf[16] === 0x6D && buf[17] === 0x70 && buf[18] === 0x34 && buf[19] === 0x31 && buf[20] === 0x6D && buf[21] === 0x70 && buf[22] === 0x34 && buf[23] === 0x32 && buf[24] === 0x69 && buf[25] === 0x73 && buf[26] === 0x6F && buf[27] === 0x6D) || 142 | (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1C && buf[4] === 0x66 && buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x69 && buf[9] === 0x73 && buf[10] === 0x6F && buf[11] === 0x6D) || 143 | (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1c && buf[4] === 0x66 && buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x6D && buf[9] === 0x70 && buf[10] === 0x34 && buf[11] === 0x32 && buf[12] === 0x0 && buf[13] === 0x0 && buf[14] === 0x0 && buf[15] === 0x0) 144 | ) { 145 | return { 146 | ext: 'mp4', 147 | mime: 'video/mp4' 148 | }; 149 | } 150 | 151 | if ((buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x1C && buf[4] === 0x66 && buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x4D && buf[9] === 0x34 && buf[10] === 0x56)) { 152 | return { 153 | ext: 'm4v', 154 | mime: 'video/x-m4v' 155 | }; 156 | } 157 | 158 | if (buf[0] === 0x4D && buf[1] === 0x54 && buf[2] === 0x68 && buf[3] === 0x64) { 159 | return { 160 | ext: 'mid', 161 | mime: 'audio/midi' 162 | }; 163 | } 164 | 165 | // needs to be before the `webm` check 166 | if (buf[31] === 0x6D && buf[32] === 0x61 && buf[33] === 0x74 && buf[34] === 0x72 && buf[35] === 0x6f && buf[36] === 0x73 && buf[37] === 0x6B && buf[38] === 0x61) { 167 | return { 168 | ext: 'mkv', 169 | mime: 'video/x-matroska' 170 | }; 171 | } 172 | 173 | if (buf[0] === 0x1A && buf[1] === 0x45 && buf[2] === 0xDF && buf[3] === 0xA3) { 174 | return { 175 | ext: 'webm', 176 | mime: 'video/webm' 177 | }; 178 | } 179 | 180 | if (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x0 && buf[3] === 0x14 && buf[4] === 0x66 && buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70) { 181 | return { 182 | ext: 'mov', 183 | mime: 'video/quicktime' 184 | }; 185 | } 186 | 187 | if (buf[0] === 0x52 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x46 && buf[8] === 0x41 && buf[9] === 0x56 && buf[10] === 0x49) { 188 | return { 189 | ext: 'avi', 190 | mime: 'video/x-msvideo' 191 | }; 192 | } 193 | 194 | if (buf[0] === 0x30 && buf[1] === 0x26 && buf[2] === 0xB2 && buf[3] === 0x75 && buf[4] === 0x8E && buf[5] === 0x66 && buf[6] === 0xCF && buf[7] === 0x11 && buf[8] === 0xA6 && buf[9] === 0xD9) { 195 | return { 196 | ext: 'wmv', 197 | mime: 'video/x-ms-wmv' 198 | }; 199 | } 200 | 201 | if (buf[0] === 0x0 && buf[1] === 0x0 && buf[2] === 0x1 && buf[3].toString(16)[0] === 'b') { 202 | return { 203 | ext: 'mpg', 204 | mime: 'video/mpeg' 205 | }; 206 | } 207 | 208 | if ((buf[0] === 0x49 && buf[1] === 0x44 && buf[2] === 0x33) || (buf[0] === 0xFF && buf[1] === 0xfb)) { 209 | return { 210 | ext: 'mp3', 211 | mime: 'audio/mpeg' 212 | }; 213 | } 214 | 215 | if ((buf[4] === 0x66 && buf[5] === 0x74 && buf[6] === 0x79 && buf[7] === 0x70 && buf[8] === 0x4D && buf[9] === 0x34 && buf[10] === 0x41) || (buf[0] === 0x4D && buf[1] === 0x34 && buf[2] === 0x41 && buf[3] === 0x20)) { 216 | return { 217 | ext: 'm4a', 218 | mime: 'audio/m4a' 219 | }; 220 | } 221 | 222 | // needs to be before `ogg` check 223 | if (buf[28] === 0x4F && buf[29] === 0x70 && buf[30] === 0x75 && buf[31] === 0x73 && buf[32] === 0x48 && buf[33] === 0x65 && buf[34] === 0x61 && buf[35] === 0x64) { 224 | return { 225 | ext: 'opus', 226 | mime: 'audio/opus' 227 | }; 228 | } 229 | 230 | if (buf[0] === 0x4F && buf[1] === 0x67 && buf[2] === 0x67 && buf[3] === 0x53) { 231 | return { 232 | ext: 'ogg', 233 | mime: 'audio/ogg' 234 | }; 235 | } 236 | 237 | if (buf[0] === 0x66 && buf[1] === 0x4C && buf[2] === 0x61 && buf[3] === 0x43) { 238 | return { 239 | ext: 'flac', 240 | mime: 'audio/x-flac' 241 | }; 242 | } 243 | 244 | if (buf[0] === 0x52 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x46 && buf[8] === 0x57 && buf[9] === 0x41 && buf[10] === 0x56 && buf[11] === 0x45) { 245 | return { 246 | ext: 'wav', 247 | mime: 'audio/x-wav' 248 | }; 249 | } 250 | 251 | if (buf[0] === 0x23 && buf[1] === 0x21 && buf[2] === 0x41 && buf[3] === 0x4D && buf[4] === 0x52 && buf[5] === 0x0A) { 252 | return { 253 | ext: 'amr', 254 | mime: 'audio/amr' 255 | }; 256 | } 257 | 258 | if (buf[0] === 0x25 && buf[1] === 0x50 && buf[2] === 0x44 && buf[3] === 0x46) { 259 | return { 260 | ext: 'pdf', 261 | mime: 'application/pdf' 262 | }; 263 | } 264 | 265 | if (buf[0] === 0x4D && buf[1] === 0x5A) { 266 | return { 267 | ext: 'exe', 268 | mime: 'application/x-msdownload' 269 | }; 270 | } 271 | 272 | if ((buf[0] === 0x43 || buf[0] === 0x46) && buf[1] === 0x57 && buf[2] === 0x53) { 273 | return { 274 | ext: 'swf', 275 | mime: 'application/x-shockwave-flash' 276 | }; 277 | } 278 | 279 | if (buf[0] === 0x7B && buf[1] === 0x5C && buf[2] === 0x72 && buf[3] === 0x74 && buf[4] === 0x66) { 280 | return { 281 | ext: 'rtf', 282 | mime: 'application/rtf' 283 | }; 284 | } 285 | 286 | if ( 287 | (buf[0] === 0x77 && buf[1] === 0x4F && buf[2] === 0x46 && buf[3] === 0x46) && 288 | ( 289 | (buf[4] === 0x00 && buf[5] === 0x01 && buf[6] === 0x00 && buf[7] === 0x00) || 290 | (buf[4] === 0x4F && buf[5] === 0x54 && buf[6] === 0x54 && buf[7] === 0x4F) 291 | ) 292 | ) { 293 | return { 294 | ext: 'woff', 295 | mime: 'application/font-woff' 296 | }; 297 | } 298 | 299 | if ( 300 | (buf[0] === 0x77 && buf[1] === 0x4F && buf[2] === 0x46 && buf[3] === 0x32) && 301 | ( 302 | (buf[4] === 0x00 && buf[5] === 0x01 && buf[6] === 0x00 && buf[7] === 0x00) || 303 | (buf[4] === 0x4F && buf[5] === 0x54 && buf[6] === 0x54 && buf[7] === 0x4F) 304 | ) 305 | ) { 306 | return { 307 | ext: 'woff2', 308 | mime: 'application/font-woff' 309 | }; 310 | } 311 | 312 | if ( 313 | (buf[34] === 0x4C && buf[35] === 0x50) && 314 | ( 315 | (buf[8] === 0x00 && buf[9] === 0x00 && buf[10] === 0x01) || 316 | (buf[8] === 0x01 && buf[9] === 0x00 && buf[10] === 0x02) || 317 | (buf[8] === 0x02 && buf[9] === 0x00 && buf[10] === 0x02) 318 | ) 319 | ) { 320 | return { 321 | ext: 'eot', 322 | mime: 'application/octet-stream' 323 | }; 324 | } 325 | 326 | if (buf[0] === 0x00 && buf[1] === 0x01 && buf[2] === 0x00 && buf[3] === 0x00 && buf[4] === 0x00) { 327 | return { 328 | ext: 'ttf', 329 | mime: 'application/font-sfnt' 330 | }; 331 | } 332 | 333 | if (buf[0] === 0x4F && buf[1] === 0x54 && buf[2] === 0x54 && buf[3] === 0x4F && buf[4] === 0x00) { 334 | return { 335 | ext: 'otf', 336 | mime: 'application/font-sfnt' 337 | }; 338 | } 339 | 340 | if (buf[0] === 0x00 && buf[1] === 0x00 && buf[2] === 0x01 && buf[3] === 0x00) { 341 | return { 342 | ext: 'ico', 343 | mime: 'image/x-icon' 344 | }; 345 | } 346 | 347 | if (buf[0] === 0x46 && buf[1] === 0x4C && buf[2] === 0x56 && buf[3] === 0x01) { 348 | return { 349 | ext: 'flv', 350 | mime: 'video/x-flv' 351 | }; 352 | } 353 | 354 | if (buf[0] === 0x25 && buf[1] === 0x21) { 355 | return { 356 | ext: 'ps', 357 | mime: 'application/postscript' 358 | }; 359 | } 360 | 361 | if (buf[0] === 0xFD && buf[1] === 0x37 && buf[2] === 0x7A && buf[3] === 0x58 && buf[4] === 0x5A && buf[5] === 0x00) { 362 | return { 363 | ext: 'xz', 364 | mime: 'application/x-xz' 365 | }; 366 | } 367 | 368 | if (buf[0] === 0x53 && buf[1] === 0x51 && buf[2] === 0x4C && buf[3] === 0x69) { 369 | return { 370 | ext: 'sqlite', 371 | mime: 'application/x-sqlite3' 372 | }; 373 | } 374 | 375 | if (buf[0] === 0x4E && buf[1] === 0x45 && buf[2] === 0x53 && buf[3] === 0x1A) { 376 | return { 377 | ext: 'nes', 378 | mime: 'application/x-nintendo-nes-rom' 379 | }; 380 | } 381 | 382 | if (buf[0] === 0x43 && buf[1] === 0x72 && buf[2] === 0x32 && buf[3] === 0x34) { 383 | return { 384 | ext: 'crx', 385 | mime: 'application/x-google-chrome-extension' 386 | }; 387 | } 388 | 389 | if ( 390 | (buf[0] === 0x4D && buf[1] === 0x53 && buf[2] === 0x43 && buf[3] === 0x46) || 391 | (buf[0] === 0x49 && buf[1] === 0x53 && buf[2] === 0x63 && buf[3] === 0x28) 392 | ) { 393 | return { 394 | ext: 'cab', 395 | mime: 'application/vnd.ms-cab-compressed' 396 | }; 397 | } 398 | 399 | // needs to be before `ar` check 400 | if (buf[0] === 0x21 && buf[1] === 0x3C && buf[2] === 0x61 && buf[3] === 0x72 && buf[4] === 0x63 && buf[5] === 0x68 && buf[6] === 0x3E && buf[7] === 0x0A && buf[8] === 0x64 && buf[9] === 0x65 && buf[10] === 0x62 && buf[11] === 0x69 && buf[12] === 0x61 && buf[13] === 0x6E && buf[14] === 0x2D && buf[15] === 0x62 && buf[16] === 0x69 && buf[17] === 0x6E && buf[18] === 0x61 && buf[19] === 0x72 && buf[20] === 0x79) { 401 | return { 402 | ext: 'deb', 403 | mime: 'application/x-deb' 404 | }; 405 | } 406 | 407 | if (buf[0] === 0x21 && buf[1] === 0x3C && buf[2] === 0x61 && buf[3] === 0x72 && buf[4] === 0x63 && buf[5] === 0x68 && buf[6] === 0x3E) { 408 | return { 409 | ext: 'ar', 410 | mime: 'application/x-unix-archive' 411 | }; 412 | } 413 | 414 | if (buf[0] === 0xED && buf[1] === 0xAB && buf[2] === 0xEE && buf[3] === 0xDB) { 415 | return { 416 | ext: 'rpm', 417 | mime: 'application/x-rpm' 418 | }; 419 | } 420 | 421 | if ( 422 | (buf[0] === 0x1F && buf[1] === 0xA0) || 423 | (buf[0] === 0x1F && buf[1] === 0x9D) 424 | ) { 425 | return { 426 | ext: 'Z', 427 | mime: 'application/x-compress' 428 | }; 429 | } 430 | 431 | if (buf[0] === 0x4C && buf[1] === 0x5A && buf[2] === 0x49 && buf[3] === 0x50) { 432 | return { 433 | ext: 'lz', 434 | mime: 'application/x-lzip' 435 | }; 436 | } 437 | 438 | if (buf[0] === 0xD0 && buf[1] === 0xCF && buf[2] === 0x11 && buf[3] === 0xE0 && buf[4] === 0xA1 && buf[5] === 0xB1 && buf[6] === 0x1A && buf[7] === 0xE1) { 439 | return { 440 | ext: 'msi', 441 | mime: 'application/x-msi' 442 | }; 443 | } 444 | 445 | return null; 446 | }; 447 | -------------------------------------------------------------------------------- /example/buffer.js: -------------------------------------------------------------------------------- 1 | (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.buffer = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 7 | * @license MIT 8 | */ 9 | /* eslint-disable no-proto */ 10 | 11 | 'use strict' 12 | 13 | var base64 = require('base64-js') 14 | var ieee754 = require('ieee754') 15 | var isArray = require('isarray') 16 | 17 | exports.Buffer = Buffer 18 | exports.SlowBuffer = SlowBuffer 19 | exports.INSPECT_MAX_BYTES = 50 20 | Buffer.poolSize = 8192 // not used by this implementation 21 | 22 | var rootParent = {} 23 | 24 | /** 25 | * If `Buffer.TYPED_ARRAY_SUPPORT`: 26 | * === true Use Uint8Array implementation (fastest) 27 | * === false Use Object implementation (most compatible, even IE6) 28 | * 29 | * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, 30 | * Opera 11.6+, iOS 4.2+. 31 | * 32 | * Due to various browser bugs, sometimes the Object implementation will be used even 33 | * when the browser supports typed arrays. 34 | * 35 | * Note: 36 | * 37 | * - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances, 38 | * See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438. 39 | * 40 | * - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function. 41 | * 42 | * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of 43 | * incorrect length in some situations. 44 | 45 | * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they 46 | * get the Object implementation, which is slower but behaves correctly. 47 | */ 48 | Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined 49 | ? global.TYPED_ARRAY_SUPPORT 50 | : typedArraySupport() 51 | 52 | function typedArraySupport () { 53 | try { 54 | var arr = new Uint8Array(1) 55 | arr.foo = function () { return 42 } 56 | return arr.foo() === 42 && // typed array instances can be augmented 57 | typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray` 58 | arr.subarray(1, 1).byteLength === 0 // ie10 has broken `subarray` 59 | } catch (e) { 60 | return false 61 | } 62 | } 63 | 64 | function kMaxLength () { 65 | return Buffer.TYPED_ARRAY_SUPPORT 66 | ? 0x7fffffff 67 | : 0x3fffffff 68 | } 69 | 70 | /** 71 | * The Buffer constructor returns instances of `Uint8Array` that have their 72 | * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of 73 | * `Uint8Array`, so the returned instances will have all the node `Buffer` methods 74 | * and the `Uint8Array` methods. Square bracket notation works as expected -- it 75 | * returns a single octet. 76 | * 77 | * The `Uint8Array` prototype remains unmodified. 78 | */ 79 | function Buffer (arg) { 80 | if (!(this instanceof Buffer)) { 81 | // Avoid going through an ArgumentsAdaptorTrampoline in the common case. 82 | if (arguments.length > 1) return new Buffer(arg, arguments[1]) 83 | return new Buffer(arg) 84 | } 85 | 86 | if (!Buffer.TYPED_ARRAY_SUPPORT) { 87 | this.length = 0 88 | this.parent = undefined 89 | } 90 | 91 | // Common case. 92 | if (typeof arg === 'number') { 93 | return fromNumber(this, arg) 94 | } 95 | 96 | // Slightly less common case. 97 | if (typeof arg === 'string') { 98 | return fromString(this, arg, arguments.length > 1 ? arguments[1] : 'utf8') 99 | } 100 | 101 | // Unusual. 102 | return fromObject(this, arg) 103 | } 104 | 105 | // TODO: Legacy, not needed anymore. Remove in next major version. 106 | Buffer._augment = function (arr) { 107 | arr.__proto__ = Buffer.prototype 108 | return arr 109 | } 110 | 111 | function fromNumber (that, length) { 112 | that = allocate(that, length < 0 ? 0 : checked(length) | 0) 113 | if (!Buffer.TYPED_ARRAY_SUPPORT) { 114 | for (var i = 0; i < length; i++) { 115 | that[i] = 0 116 | } 117 | } 118 | return that 119 | } 120 | 121 | function fromString (that, string, encoding) { 122 | if (typeof encoding !== 'string' || encoding === '') encoding = 'utf8' 123 | 124 | // Assumption: byteLength() return value is always < kMaxLength. 125 | var length = byteLength(string, encoding) | 0 126 | that = allocate(that, length) 127 | 128 | that.write(string, encoding) 129 | return that 130 | } 131 | 132 | function fromObject (that, object) { 133 | if (Buffer.isBuffer(object)) return fromBuffer(that, object) 134 | 135 | if (isArray(object)) return fromArray(that, object) 136 | 137 | if (object == null) { 138 | throw new TypeError('must start with number, buffer, array or string') 139 | } 140 | 141 | if (typeof ArrayBuffer !== 'undefined') { 142 | if (object.buffer instanceof ArrayBuffer) { 143 | return fromTypedArray(that, object) 144 | } 145 | if (object instanceof ArrayBuffer) { 146 | return fromArrayBuffer(that, object) 147 | } 148 | } 149 | 150 | if (object.length) return fromArrayLike(that, object) 151 | 152 | return fromJsonObject(that, object) 153 | } 154 | 155 | function fromBuffer (that, buffer) { 156 | var length = checked(buffer.length) | 0 157 | that = allocate(that, length) 158 | buffer.copy(that, 0, 0, length) 159 | return that 160 | } 161 | 162 | function fromArray (that, array) { 163 | var length = checked(array.length) | 0 164 | that = allocate(that, length) 165 | for (var i = 0; i < length; i += 1) { 166 | that[i] = array[i] & 255 167 | } 168 | return that 169 | } 170 | 171 | // Duplicate of fromArray() to keep fromArray() monomorphic. 172 | function fromTypedArray (that, array) { 173 | var length = checked(array.length) | 0 174 | that = allocate(that, length) 175 | // Truncating the elements is probably not what people expect from typed 176 | // arrays with BYTES_PER_ELEMENT > 1 but it's compatible with the behavior 177 | // of the old Buffer constructor. 178 | for (var i = 0; i < length; i += 1) { 179 | that[i] = array[i] & 255 180 | } 181 | return that 182 | } 183 | 184 | function fromArrayBuffer (that, array) { 185 | array.byteLength // this throws if `array` is not a valid ArrayBuffer 186 | 187 | if (Buffer.TYPED_ARRAY_SUPPORT) { 188 | // Return an augmented `Uint8Array` instance, for best performance 189 | that = new Uint8Array(array) 190 | that.__proto__ = Buffer.prototype 191 | } else { 192 | // Fallback: Return an object instance of the Buffer class 193 | that = fromTypedArray(that, new Uint8Array(array)) 194 | } 195 | return that 196 | } 197 | 198 | function fromArrayLike (that, array) { 199 | var length = checked(array.length) | 0 200 | that = allocate(that, length) 201 | for (var i = 0; i < length; i += 1) { 202 | that[i] = array[i] & 255 203 | } 204 | return that 205 | } 206 | 207 | // Deserialize { type: 'Buffer', data: [1,2,3,...] } into a Buffer object. 208 | // Returns a zero-length buffer for inputs that don't conform to the spec. 209 | function fromJsonObject (that, object) { 210 | var array 211 | var length = 0 212 | 213 | if (object.type === 'Buffer' && isArray(object.data)) { 214 | array = object.data 215 | length = checked(array.length) | 0 216 | } 217 | that = allocate(that, length) 218 | 219 | for (var i = 0; i < length; i += 1) { 220 | that[i] = array[i] & 255 221 | } 222 | return that 223 | } 224 | 225 | if (Buffer.TYPED_ARRAY_SUPPORT) { 226 | Buffer.prototype.__proto__ = Uint8Array.prototype 227 | Buffer.__proto__ = Uint8Array 228 | if (typeof Symbol !== 'undefined' && Symbol.species && 229 | Buffer[Symbol.species] === Buffer) { 230 | // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 231 | Object.defineProperty(Buffer, Symbol.species, { 232 | value: null, 233 | configurable: true 234 | }) 235 | } 236 | } else { 237 | // pre-set for values that may exist in the future 238 | Buffer.prototype.length = undefined 239 | Buffer.prototype.parent = undefined 240 | } 241 | 242 | function allocate (that, length) { 243 | if (Buffer.TYPED_ARRAY_SUPPORT) { 244 | // Return an augmented `Uint8Array` instance, for best performance 245 | that = new Uint8Array(length) 246 | that.__proto__ = Buffer.prototype 247 | } else { 248 | // Fallback: Return an object instance of the Buffer class 249 | that.length = length 250 | } 251 | 252 | var fromPool = length !== 0 && length <= Buffer.poolSize >>> 1 253 | if (fromPool) that.parent = rootParent 254 | 255 | return that 256 | } 257 | 258 | function checked (length) { 259 | // Note: cannot use `length < kMaxLength` here because that fails when 260 | // length is NaN (which is otherwise coerced to zero.) 261 | if (length >= kMaxLength()) { 262 | throw new RangeError('Attempt to allocate Buffer larger than maximum ' + 263 | 'size: 0x' + kMaxLength().toString(16) + ' bytes') 264 | } 265 | return length | 0 266 | } 267 | 268 | function SlowBuffer (subject, encoding) { 269 | if (!(this instanceof SlowBuffer)) return new SlowBuffer(subject, encoding) 270 | 271 | var buf = new Buffer(subject, encoding) 272 | delete buf.parent 273 | return buf 274 | } 275 | 276 | Buffer.isBuffer = function isBuffer (b) { 277 | return !!(b != null && b._isBuffer) 278 | } 279 | 280 | Buffer.compare = function compare (a, b) { 281 | if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { 282 | throw new TypeError('Arguments must be Buffers') 283 | } 284 | 285 | if (a === b) return 0 286 | 287 | var x = a.length 288 | var y = b.length 289 | 290 | for (var i = 0, len = Math.min(x, y); i < len; ++i) { 291 | if (a[i] !== b[i]) { 292 | x = a[i] 293 | y = b[i] 294 | break 295 | } 296 | } 297 | 298 | if (x < y) return -1 299 | if (y < x) return 1 300 | return 0 301 | } 302 | 303 | Buffer.isEncoding = function isEncoding (encoding) { 304 | switch (String(encoding).toLowerCase()) { 305 | case 'hex': 306 | case 'utf8': 307 | case 'utf-8': 308 | case 'ascii': 309 | case 'binary': 310 | case 'base64': 311 | case 'raw': 312 | case 'ucs2': 313 | case 'ucs-2': 314 | case 'utf16le': 315 | case 'utf-16le': 316 | return true 317 | default: 318 | return false 319 | } 320 | } 321 | 322 | Buffer.concat = function concat (list, length) { 323 | if (!isArray(list)) throw new TypeError('list argument must be an Array of Buffers.') 324 | 325 | if (list.length === 0) { 326 | return new Buffer(0) 327 | } 328 | 329 | var i 330 | if (length === undefined) { 331 | length = 0 332 | for (i = 0; i < list.length; i++) { 333 | length += list[i].length 334 | } 335 | } 336 | 337 | var buf = new Buffer(length) 338 | var pos = 0 339 | for (i = 0; i < list.length; i++) { 340 | var item = list[i] 341 | item.copy(buf, pos) 342 | pos += item.length 343 | } 344 | return buf 345 | } 346 | 347 | function byteLength (string, encoding) { 348 | if (typeof string !== 'string') string = '' + string 349 | 350 | var len = string.length 351 | if (len === 0) return 0 352 | 353 | // Use a for loop to avoid recursion 354 | var loweredCase = false 355 | for (;;) { 356 | switch (encoding) { 357 | case 'ascii': 358 | case 'binary': 359 | // Deprecated 360 | case 'raw': 361 | case 'raws': 362 | return len 363 | case 'utf8': 364 | case 'utf-8': 365 | return utf8ToBytes(string).length 366 | case 'ucs2': 367 | case 'ucs-2': 368 | case 'utf16le': 369 | case 'utf-16le': 370 | return len * 2 371 | case 'hex': 372 | return len >>> 1 373 | case 'base64': 374 | return base64ToBytes(string).length 375 | default: 376 | if (loweredCase) return utf8ToBytes(string).length // assume utf8 377 | encoding = ('' + encoding).toLowerCase() 378 | loweredCase = true 379 | } 380 | } 381 | } 382 | Buffer.byteLength = byteLength 383 | 384 | function slowToString (encoding, start, end) { 385 | var loweredCase = false 386 | 387 | start = start | 0 388 | end = end === undefined || end === Infinity ? this.length : end | 0 389 | 390 | if (!encoding) encoding = 'utf8' 391 | if (start < 0) start = 0 392 | if (end > this.length) end = this.length 393 | if (end <= start) return '' 394 | 395 | while (true) { 396 | switch (encoding) { 397 | case 'hex': 398 | return hexSlice(this, start, end) 399 | 400 | case 'utf8': 401 | case 'utf-8': 402 | return utf8Slice(this, start, end) 403 | 404 | case 'ascii': 405 | return asciiSlice(this, start, end) 406 | 407 | case 'binary': 408 | return binarySlice(this, start, end) 409 | 410 | case 'base64': 411 | return base64Slice(this, start, end) 412 | 413 | case 'ucs2': 414 | case 'ucs-2': 415 | case 'utf16le': 416 | case 'utf-16le': 417 | return utf16leSlice(this, start, end) 418 | 419 | default: 420 | if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) 421 | encoding = (encoding + '').toLowerCase() 422 | loweredCase = true 423 | } 424 | } 425 | } 426 | 427 | // The property is used by `Buffer.isBuffer` and `is-buffer` (in Safari 5-7) to detect 428 | // Buffer instances. 429 | Buffer.prototype._isBuffer = true 430 | 431 | Buffer.prototype.toString = function toString () { 432 | var length = this.length | 0 433 | if (length === 0) return '' 434 | if (arguments.length === 0) return utf8Slice(this, 0, length) 435 | return slowToString.apply(this, arguments) 436 | } 437 | 438 | Buffer.prototype.equals = function equals (b) { 439 | if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') 440 | if (this === b) return true 441 | return Buffer.compare(this, b) === 0 442 | } 443 | 444 | Buffer.prototype.inspect = function inspect () { 445 | var str = '' 446 | var max = exports.INSPECT_MAX_BYTES 447 | if (this.length > 0) { 448 | str = this.toString('hex', 0, max).match(/.{2}/g).join(' ') 449 | if (this.length > max) str += ' ... ' 450 | } 451 | return '' 452 | } 453 | 454 | Buffer.prototype.compare = function compare (b) { 455 | if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') 456 | return Buffer.compare(this, b) 457 | } 458 | 459 | Buffer.prototype.indexOf = function indexOf (val, byteOffset) { 460 | if (byteOffset > 0x7fffffff) byteOffset = 0x7fffffff 461 | else if (byteOffset < -0x80000000) byteOffset = -0x80000000 462 | byteOffset >>= 0 463 | 464 | if (this.length === 0) return -1 465 | if (byteOffset >= this.length) return -1 466 | 467 | // Negative offsets start from the end of the buffer 468 | if (byteOffset < 0) byteOffset = Math.max(this.length + byteOffset, 0) 469 | 470 | if (typeof val === 'string') { 471 | if (val.length === 0) return -1 // special case: looking for empty string always fails 472 | return String.prototype.indexOf.call(this, val, byteOffset) 473 | } 474 | if (Buffer.isBuffer(val)) { 475 | return arrayIndexOf(this, val, byteOffset) 476 | } 477 | if (typeof val === 'number') { 478 | if (Buffer.TYPED_ARRAY_SUPPORT && Uint8Array.prototype.indexOf === 'function') { 479 | return Uint8Array.prototype.indexOf.call(this, val, byteOffset) 480 | } 481 | return arrayIndexOf(this, [ val ], byteOffset) 482 | } 483 | 484 | function arrayIndexOf (arr, val, byteOffset) { 485 | var foundIndex = -1 486 | for (var i = 0; byteOffset + i < arr.length; i++) { 487 | if (arr[byteOffset + i] === val[foundIndex === -1 ? 0 : i - foundIndex]) { 488 | if (foundIndex === -1) foundIndex = i 489 | if (i - foundIndex + 1 === val.length) return byteOffset + foundIndex 490 | } else { 491 | foundIndex = -1 492 | } 493 | } 494 | return -1 495 | } 496 | 497 | throw new TypeError('val must be string, number or Buffer') 498 | } 499 | 500 | function hexWrite (buf, string, offset, length) { 501 | offset = Number(offset) || 0 502 | var remaining = buf.length - offset 503 | if (!length) { 504 | length = remaining 505 | } else { 506 | length = Number(length) 507 | if (length > remaining) { 508 | length = remaining 509 | } 510 | } 511 | 512 | // must be an even number of digits 513 | var strLen = string.length 514 | if (strLen % 2 !== 0) throw new Error('Invalid hex string') 515 | 516 | if (length > strLen / 2) { 517 | length = strLen / 2 518 | } 519 | for (var i = 0; i < length; i++) { 520 | var parsed = parseInt(string.substr(i * 2, 2), 16) 521 | if (isNaN(parsed)) throw new Error('Invalid hex string') 522 | buf[offset + i] = parsed 523 | } 524 | return i 525 | } 526 | 527 | function utf8Write (buf, string, offset, length) { 528 | return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) 529 | } 530 | 531 | function asciiWrite (buf, string, offset, length) { 532 | return blitBuffer(asciiToBytes(string), buf, offset, length) 533 | } 534 | 535 | function binaryWrite (buf, string, offset, length) { 536 | return asciiWrite(buf, string, offset, length) 537 | } 538 | 539 | function base64Write (buf, string, offset, length) { 540 | return blitBuffer(base64ToBytes(string), buf, offset, length) 541 | } 542 | 543 | function ucs2Write (buf, string, offset, length) { 544 | return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) 545 | } 546 | 547 | Buffer.prototype.write = function write (string, offset, length, encoding) { 548 | // Buffer#write(string) 549 | if (offset === undefined) { 550 | encoding = 'utf8' 551 | length = this.length 552 | offset = 0 553 | // Buffer#write(string, encoding) 554 | } else if (length === undefined && typeof offset === 'string') { 555 | encoding = offset 556 | length = this.length 557 | offset = 0 558 | // Buffer#write(string, offset[, length][, encoding]) 559 | } else if (isFinite(offset)) { 560 | offset = offset | 0 561 | if (isFinite(length)) { 562 | length = length | 0 563 | if (encoding === undefined) encoding = 'utf8' 564 | } else { 565 | encoding = length 566 | length = undefined 567 | } 568 | // legacy write(string, encoding, offset, length) - remove in v0.13 569 | } else { 570 | var swap = encoding 571 | encoding = offset 572 | offset = length | 0 573 | length = swap 574 | } 575 | 576 | var remaining = this.length - offset 577 | if (length === undefined || length > remaining) length = remaining 578 | 579 | if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { 580 | throw new RangeError('attempt to write outside buffer bounds') 581 | } 582 | 583 | if (!encoding) encoding = 'utf8' 584 | 585 | var loweredCase = false 586 | for (;;) { 587 | switch (encoding) { 588 | case 'hex': 589 | return hexWrite(this, string, offset, length) 590 | 591 | case 'utf8': 592 | case 'utf-8': 593 | return utf8Write(this, string, offset, length) 594 | 595 | case 'ascii': 596 | return asciiWrite(this, string, offset, length) 597 | 598 | case 'binary': 599 | return binaryWrite(this, string, offset, length) 600 | 601 | case 'base64': 602 | // Warning: maxLength not taken into account in base64Write 603 | return base64Write(this, string, offset, length) 604 | 605 | case 'ucs2': 606 | case 'ucs-2': 607 | case 'utf16le': 608 | case 'utf-16le': 609 | return ucs2Write(this, string, offset, length) 610 | 611 | default: 612 | if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) 613 | encoding = ('' + encoding).toLowerCase() 614 | loweredCase = true 615 | } 616 | } 617 | } 618 | 619 | Buffer.prototype.toJSON = function toJSON () { 620 | return { 621 | type: 'Buffer', 622 | data: Array.prototype.slice.call(this._arr || this, 0) 623 | } 624 | } 625 | 626 | function base64Slice (buf, start, end) { 627 | if (start === 0 && end === buf.length) { 628 | return base64.fromByteArray(buf) 629 | } else { 630 | return base64.fromByteArray(buf.slice(start, end)) 631 | } 632 | } 633 | 634 | function utf8Slice (buf, start, end) { 635 | end = Math.min(buf.length, end) 636 | var res = [] 637 | 638 | var i = start 639 | while (i < end) { 640 | var firstByte = buf[i] 641 | var codePoint = null 642 | var bytesPerSequence = (firstByte > 0xEF) ? 4 643 | : (firstByte > 0xDF) ? 3 644 | : (firstByte > 0xBF) ? 2 645 | : 1 646 | 647 | if (i + bytesPerSequence <= end) { 648 | var secondByte, thirdByte, fourthByte, tempCodePoint 649 | 650 | switch (bytesPerSequence) { 651 | case 1: 652 | if (firstByte < 0x80) { 653 | codePoint = firstByte 654 | } 655 | break 656 | case 2: 657 | secondByte = buf[i + 1] 658 | if ((secondByte & 0xC0) === 0x80) { 659 | tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F) 660 | if (tempCodePoint > 0x7F) { 661 | codePoint = tempCodePoint 662 | } 663 | } 664 | break 665 | case 3: 666 | secondByte = buf[i + 1] 667 | thirdByte = buf[i + 2] 668 | if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { 669 | tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F) 670 | if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { 671 | codePoint = tempCodePoint 672 | } 673 | } 674 | break 675 | case 4: 676 | secondByte = buf[i + 1] 677 | thirdByte = buf[i + 2] 678 | fourthByte = buf[i + 3] 679 | if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { 680 | tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F) 681 | if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { 682 | codePoint = tempCodePoint 683 | } 684 | } 685 | } 686 | } 687 | 688 | if (codePoint === null) { 689 | // we did not generate a valid codePoint so insert a 690 | // replacement char (U+FFFD) and advance only 1 byte 691 | codePoint = 0xFFFD 692 | bytesPerSequence = 1 693 | } else if (codePoint > 0xFFFF) { 694 | // encode to utf16 (surrogate pair dance) 695 | codePoint -= 0x10000 696 | res.push(codePoint >>> 10 & 0x3FF | 0xD800) 697 | codePoint = 0xDC00 | codePoint & 0x3FF 698 | } 699 | 700 | res.push(codePoint) 701 | i += bytesPerSequence 702 | } 703 | 704 | return decodeCodePointsArray(res) 705 | } 706 | 707 | // Based on http://stackoverflow.com/a/22747272/680742, the browser with 708 | // the lowest limit is Chrome, with 0x10000 args. 709 | // We go 1 magnitude less, for safety 710 | var MAX_ARGUMENTS_LENGTH = 0x1000 711 | 712 | function decodeCodePointsArray (codePoints) { 713 | var len = codePoints.length 714 | if (len <= MAX_ARGUMENTS_LENGTH) { 715 | return String.fromCharCode.apply(String, codePoints) // avoid extra slice() 716 | } 717 | 718 | // Decode in chunks to avoid "call stack size exceeded". 719 | var res = '' 720 | var i = 0 721 | while (i < len) { 722 | res += String.fromCharCode.apply( 723 | String, 724 | codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) 725 | ) 726 | } 727 | return res 728 | } 729 | 730 | function asciiSlice (buf, start, end) { 731 | var ret = '' 732 | end = Math.min(buf.length, end) 733 | 734 | for (var i = start; i < end; i++) { 735 | ret += String.fromCharCode(buf[i] & 0x7F) 736 | } 737 | return ret 738 | } 739 | 740 | function binarySlice (buf, start, end) { 741 | var ret = '' 742 | end = Math.min(buf.length, end) 743 | 744 | for (var i = start; i < end; i++) { 745 | ret += String.fromCharCode(buf[i]) 746 | } 747 | return ret 748 | } 749 | 750 | function hexSlice (buf, start, end) { 751 | var len = buf.length 752 | 753 | if (!start || start < 0) start = 0 754 | if (!end || end < 0 || end > len) end = len 755 | 756 | var out = '' 757 | for (var i = start; i < end; i++) { 758 | out += toHex(buf[i]) 759 | } 760 | return out 761 | } 762 | 763 | function utf16leSlice (buf, start, end) { 764 | var bytes = buf.slice(start, end) 765 | var res = '' 766 | for (var i = 0; i < bytes.length; i += 2) { 767 | res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256) 768 | } 769 | return res 770 | } 771 | 772 | Buffer.prototype.slice = function slice (start, end) { 773 | var len = this.length 774 | start = ~~start 775 | end = end === undefined ? len : ~~end 776 | 777 | if (start < 0) { 778 | start += len 779 | if (start < 0) start = 0 780 | } else if (start > len) { 781 | start = len 782 | } 783 | 784 | if (end < 0) { 785 | end += len 786 | if (end < 0) end = 0 787 | } else if (end > len) { 788 | end = len 789 | } 790 | 791 | if (end < start) end = start 792 | 793 | var newBuf 794 | if (Buffer.TYPED_ARRAY_SUPPORT) { 795 | newBuf = this.subarray(start, end) 796 | newBuf.__proto__ = Buffer.prototype 797 | } else { 798 | var sliceLen = end - start 799 | newBuf = new Buffer(sliceLen, undefined) 800 | for (var i = 0; i < sliceLen; i++) { 801 | newBuf[i] = this[i + start] 802 | } 803 | } 804 | 805 | if (newBuf.length) newBuf.parent = this.parent || this 806 | 807 | return newBuf 808 | } 809 | 810 | /* 811 | * Need to make sure that buffer isn't trying to write out of bounds. 812 | */ 813 | function checkOffset (offset, ext, length) { 814 | if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') 815 | if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') 816 | } 817 | 818 | Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { 819 | offset = offset | 0 820 | byteLength = byteLength | 0 821 | if (!noAssert) checkOffset(offset, byteLength, this.length) 822 | 823 | var val = this[offset] 824 | var mul = 1 825 | var i = 0 826 | while (++i < byteLength && (mul *= 0x100)) { 827 | val += this[offset + i] * mul 828 | } 829 | 830 | return val 831 | } 832 | 833 | Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { 834 | offset = offset | 0 835 | byteLength = byteLength | 0 836 | if (!noAssert) { 837 | checkOffset(offset, byteLength, this.length) 838 | } 839 | 840 | var val = this[offset + --byteLength] 841 | var mul = 1 842 | while (byteLength > 0 && (mul *= 0x100)) { 843 | val += this[offset + --byteLength] * mul 844 | } 845 | 846 | return val 847 | } 848 | 849 | Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { 850 | if (!noAssert) checkOffset(offset, 1, this.length) 851 | return this[offset] 852 | } 853 | 854 | Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { 855 | if (!noAssert) checkOffset(offset, 2, this.length) 856 | return this[offset] | (this[offset + 1] << 8) 857 | } 858 | 859 | Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { 860 | if (!noAssert) checkOffset(offset, 2, this.length) 861 | return (this[offset] << 8) | this[offset + 1] 862 | } 863 | 864 | Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { 865 | if (!noAssert) checkOffset(offset, 4, this.length) 866 | 867 | return ((this[offset]) | 868 | (this[offset + 1] << 8) | 869 | (this[offset + 2] << 16)) + 870 | (this[offset + 3] * 0x1000000) 871 | } 872 | 873 | Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { 874 | if (!noAssert) checkOffset(offset, 4, this.length) 875 | 876 | return (this[offset] * 0x1000000) + 877 | ((this[offset + 1] << 16) | 878 | (this[offset + 2] << 8) | 879 | this[offset + 3]) 880 | } 881 | 882 | Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { 883 | offset = offset | 0 884 | byteLength = byteLength | 0 885 | if (!noAssert) checkOffset(offset, byteLength, this.length) 886 | 887 | var val = this[offset] 888 | var mul = 1 889 | var i = 0 890 | while (++i < byteLength && (mul *= 0x100)) { 891 | val += this[offset + i] * mul 892 | } 893 | mul *= 0x80 894 | 895 | if (val >= mul) val -= Math.pow(2, 8 * byteLength) 896 | 897 | return val 898 | } 899 | 900 | Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { 901 | offset = offset | 0 902 | byteLength = byteLength | 0 903 | if (!noAssert) checkOffset(offset, byteLength, this.length) 904 | 905 | var i = byteLength 906 | var mul = 1 907 | var val = this[offset + --i] 908 | while (i > 0 && (mul *= 0x100)) { 909 | val += this[offset + --i] * mul 910 | } 911 | mul *= 0x80 912 | 913 | if (val >= mul) val -= Math.pow(2, 8 * byteLength) 914 | 915 | return val 916 | } 917 | 918 | Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { 919 | if (!noAssert) checkOffset(offset, 1, this.length) 920 | if (!(this[offset] & 0x80)) return (this[offset]) 921 | return ((0xff - this[offset] + 1) * -1) 922 | } 923 | 924 | Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { 925 | if (!noAssert) checkOffset(offset, 2, this.length) 926 | var val = this[offset] | (this[offset + 1] << 8) 927 | return (val & 0x8000) ? val | 0xFFFF0000 : val 928 | } 929 | 930 | Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { 931 | if (!noAssert) checkOffset(offset, 2, this.length) 932 | var val = this[offset + 1] | (this[offset] << 8) 933 | return (val & 0x8000) ? val | 0xFFFF0000 : val 934 | } 935 | 936 | Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { 937 | if (!noAssert) checkOffset(offset, 4, this.length) 938 | 939 | return (this[offset]) | 940 | (this[offset + 1] << 8) | 941 | (this[offset + 2] << 16) | 942 | (this[offset + 3] << 24) 943 | } 944 | 945 | Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { 946 | if (!noAssert) checkOffset(offset, 4, this.length) 947 | 948 | return (this[offset] << 24) | 949 | (this[offset + 1] << 16) | 950 | (this[offset + 2] << 8) | 951 | (this[offset + 3]) 952 | } 953 | 954 | Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { 955 | if (!noAssert) checkOffset(offset, 4, this.length) 956 | return ieee754.read(this, offset, true, 23, 4) 957 | } 958 | 959 | Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { 960 | if (!noAssert) checkOffset(offset, 4, this.length) 961 | return ieee754.read(this, offset, false, 23, 4) 962 | } 963 | 964 | Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { 965 | if (!noAssert) checkOffset(offset, 8, this.length) 966 | return ieee754.read(this, offset, true, 52, 8) 967 | } 968 | 969 | Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { 970 | if (!noAssert) checkOffset(offset, 8, this.length) 971 | return ieee754.read(this, offset, false, 52, 8) 972 | } 973 | 974 | function checkInt (buf, value, offset, ext, max, min) { 975 | if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance') 976 | if (value > max || value < min) throw new RangeError('value is out of bounds') 977 | if (offset + ext > buf.length) throw new RangeError('index out of range') 978 | } 979 | 980 | Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { 981 | value = +value 982 | offset = offset | 0 983 | byteLength = byteLength | 0 984 | if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) 985 | 986 | var mul = 1 987 | var i = 0 988 | this[offset] = value & 0xFF 989 | while (++i < byteLength && (mul *= 0x100)) { 990 | this[offset + i] = (value / mul) & 0xFF 991 | } 992 | 993 | return offset + byteLength 994 | } 995 | 996 | Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { 997 | value = +value 998 | offset = offset | 0 999 | byteLength = byteLength | 0 1000 | if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) 1001 | 1002 | var i = byteLength - 1 1003 | var mul = 1 1004 | this[offset + i] = value & 0xFF 1005 | while (--i >= 0 && (mul *= 0x100)) { 1006 | this[offset + i] = (value / mul) & 0xFF 1007 | } 1008 | 1009 | return offset + byteLength 1010 | } 1011 | 1012 | Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { 1013 | value = +value 1014 | offset = offset | 0 1015 | if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) 1016 | if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) 1017 | this[offset] = (value & 0xff) 1018 | return offset + 1 1019 | } 1020 | 1021 | function objectWriteUInt16 (buf, value, offset, littleEndian) { 1022 | if (value < 0) value = 0xffff + value + 1 1023 | for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) { 1024 | buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>> 1025 | (littleEndian ? i : 1 - i) * 8 1026 | } 1027 | } 1028 | 1029 | Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { 1030 | value = +value 1031 | offset = offset | 0 1032 | if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) 1033 | if (Buffer.TYPED_ARRAY_SUPPORT) { 1034 | this[offset] = (value & 0xff) 1035 | this[offset + 1] = (value >>> 8) 1036 | } else { 1037 | objectWriteUInt16(this, value, offset, true) 1038 | } 1039 | return offset + 2 1040 | } 1041 | 1042 | Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { 1043 | value = +value 1044 | offset = offset | 0 1045 | if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) 1046 | if (Buffer.TYPED_ARRAY_SUPPORT) { 1047 | this[offset] = (value >>> 8) 1048 | this[offset + 1] = (value & 0xff) 1049 | } else { 1050 | objectWriteUInt16(this, value, offset, false) 1051 | } 1052 | return offset + 2 1053 | } 1054 | 1055 | function objectWriteUInt32 (buf, value, offset, littleEndian) { 1056 | if (value < 0) value = 0xffffffff + value + 1 1057 | for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) { 1058 | buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff 1059 | } 1060 | } 1061 | 1062 | Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { 1063 | value = +value 1064 | offset = offset | 0 1065 | if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) 1066 | if (Buffer.TYPED_ARRAY_SUPPORT) { 1067 | this[offset + 3] = (value >>> 24) 1068 | this[offset + 2] = (value >>> 16) 1069 | this[offset + 1] = (value >>> 8) 1070 | this[offset] = (value & 0xff) 1071 | } else { 1072 | objectWriteUInt32(this, value, offset, true) 1073 | } 1074 | return offset + 4 1075 | } 1076 | 1077 | Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { 1078 | value = +value 1079 | offset = offset | 0 1080 | if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) 1081 | if (Buffer.TYPED_ARRAY_SUPPORT) { 1082 | this[offset] = (value >>> 24) 1083 | this[offset + 1] = (value >>> 16) 1084 | this[offset + 2] = (value >>> 8) 1085 | this[offset + 3] = (value & 0xff) 1086 | } else { 1087 | objectWriteUInt32(this, value, offset, false) 1088 | } 1089 | return offset + 4 1090 | } 1091 | 1092 | Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { 1093 | value = +value 1094 | offset = offset | 0 1095 | if (!noAssert) { 1096 | var limit = Math.pow(2, 8 * byteLength - 1) 1097 | 1098 | checkInt(this, value, offset, byteLength, limit - 1, -limit) 1099 | } 1100 | 1101 | var i = 0 1102 | var mul = 1 1103 | var sub = value < 0 ? 1 : 0 1104 | this[offset] = value & 0xFF 1105 | while (++i < byteLength && (mul *= 0x100)) { 1106 | this[offset + i] = ((value / mul) >> 0) - sub & 0xFF 1107 | } 1108 | 1109 | return offset + byteLength 1110 | } 1111 | 1112 | Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { 1113 | value = +value 1114 | offset = offset | 0 1115 | if (!noAssert) { 1116 | var limit = Math.pow(2, 8 * byteLength - 1) 1117 | 1118 | checkInt(this, value, offset, byteLength, limit - 1, -limit) 1119 | } 1120 | 1121 | var i = byteLength - 1 1122 | var mul = 1 1123 | var sub = value < 0 ? 1 : 0 1124 | this[offset + i] = value & 0xFF 1125 | while (--i >= 0 && (mul *= 0x100)) { 1126 | this[offset + i] = ((value / mul) >> 0) - sub & 0xFF 1127 | } 1128 | 1129 | return offset + byteLength 1130 | } 1131 | 1132 | Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { 1133 | value = +value 1134 | offset = offset | 0 1135 | if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) 1136 | if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) 1137 | if (value < 0) value = 0xff + value + 1 1138 | this[offset] = (value & 0xff) 1139 | return offset + 1 1140 | } 1141 | 1142 | Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { 1143 | value = +value 1144 | offset = offset | 0 1145 | if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) 1146 | if (Buffer.TYPED_ARRAY_SUPPORT) { 1147 | this[offset] = (value & 0xff) 1148 | this[offset + 1] = (value >>> 8) 1149 | } else { 1150 | objectWriteUInt16(this, value, offset, true) 1151 | } 1152 | return offset + 2 1153 | } 1154 | 1155 | Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { 1156 | value = +value 1157 | offset = offset | 0 1158 | if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) 1159 | if (Buffer.TYPED_ARRAY_SUPPORT) { 1160 | this[offset] = (value >>> 8) 1161 | this[offset + 1] = (value & 0xff) 1162 | } else { 1163 | objectWriteUInt16(this, value, offset, false) 1164 | } 1165 | return offset + 2 1166 | } 1167 | 1168 | Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { 1169 | value = +value 1170 | offset = offset | 0 1171 | if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) 1172 | if (Buffer.TYPED_ARRAY_SUPPORT) { 1173 | this[offset] = (value & 0xff) 1174 | this[offset + 1] = (value >>> 8) 1175 | this[offset + 2] = (value >>> 16) 1176 | this[offset + 3] = (value >>> 24) 1177 | } else { 1178 | objectWriteUInt32(this, value, offset, true) 1179 | } 1180 | return offset + 4 1181 | } 1182 | 1183 | Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { 1184 | value = +value 1185 | offset = offset | 0 1186 | if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) 1187 | if (value < 0) value = 0xffffffff + value + 1 1188 | if (Buffer.TYPED_ARRAY_SUPPORT) { 1189 | this[offset] = (value >>> 24) 1190 | this[offset + 1] = (value >>> 16) 1191 | this[offset + 2] = (value >>> 8) 1192 | this[offset + 3] = (value & 0xff) 1193 | } else { 1194 | objectWriteUInt32(this, value, offset, false) 1195 | } 1196 | return offset + 4 1197 | } 1198 | 1199 | function checkIEEE754 (buf, value, offset, ext, max, min) { 1200 | if (offset + ext > buf.length) throw new RangeError('index out of range') 1201 | if (offset < 0) throw new RangeError('index out of range') 1202 | } 1203 | 1204 | function writeFloat (buf, value, offset, littleEndian, noAssert) { 1205 | if (!noAssert) { 1206 | checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) 1207 | } 1208 | ieee754.write(buf, value, offset, littleEndian, 23, 4) 1209 | return offset + 4 1210 | } 1211 | 1212 | Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { 1213 | return writeFloat(this, value, offset, true, noAssert) 1214 | } 1215 | 1216 | Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { 1217 | return writeFloat(this, value, offset, false, noAssert) 1218 | } 1219 | 1220 | function writeDouble (buf, value, offset, littleEndian, noAssert) { 1221 | if (!noAssert) { 1222 | checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) 1223 | } 1224 | ieee754.write(buf, value, offset, littleEndian, 52, 8) 1225 | return offset + 8 1226 | } 1227 | 1228 | Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { 1229 | return writeDouble(this, value, offset, true, noAssert) 1230 | } 1231 | 1232 | Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { 1233 | return writeDouble(this, value, offset, false, noAssert) 1234 | } 1235 | 1236 | // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) 1237 | Buffer.prototype.copy = function copy (target, targetStart, start, end) { 1238 | if (!start) start = 0 1239 | if (!end && end !== 0) end = this.length 1240 | if (targetStart >= target.length) targetStart = target.length 1241 | if (!targetStart) targetStart = 0 1242 | if (end > 0 && end < start) end = start 1243 | 1244 | // Copy 0 bytes; we're done 1245 | if (end === start) return 0 1246 | if (target.length === 0 || this.length === 0) return 0 1247 | 1248 | // Fatal error conditions 1249 | if (targetStart < 0) { 1250 | throw new RangeError('targetStart out of bounds') 1251 | } 1252 | if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds') 1253 | if (end < 0) throw new RangeError('sourceEnd out of bounds') 1254 | 1255 | // Are we oob? 1256 | if (end > this.length) end = this.length 1257 | if (target.length - targetStart < end - start) { 1258 | end = target.length - targetStart + start 1259 | } 1260 | 1261 | var len = end - start 1262 | var i 1263 | 1264 | if (this === target && start < targetStart && targetStart < end) { 1265 | // descending copy from end 1266 | for (i = len - 1; i >= 0; i--) { 1267 | target[i + targetStart] = this[i + start] 1268 | } 1269 | } else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) { 1270 | // ascending copy from start 1271 | for (i = 0; i < len; i++) { 1272 | target[i + targetStart] = this[i + start] 1273 | } 1274 | } else { 1275 | Uint8Array.prototype.set.call( 1276 | target, 1277 | this.subarray(start, start + len), 1278 | targetStart 1279 | ) 1280 | } 1281 | 1282 | return len 1283 | } 1284 | 1285 | // fill(value, start=0, end=buffer.length) 1286 | Buffer.prototype.fill = function fill (value, start, end) { 1287 | if (!value) value = 0 1288 | if (!start) start = 0 1289 | if (!end) end = this.length 1290 | 1291 | if (end < start) throw new RangeError('end < start') 1292 | 1293 | // Fill 0 bytes; we're done 1294 | if (end === start) return 1295 | if (this.length === 0) return 1296 | 1297 | if (start < 0 || start >= this.length) throw new RangeError('start out of bounds') 1298 | if (end < 0 || end > this.length) throw new RangeError('end out of bounds') 1299 | 1300 | var i 1301 | if (typeof value === 'number') { 1302 | for (i = start; i < end; i++) { 1303 | this[i] = value 1304 | } 1305 | } else { 1306 | var bytes = utf8ToBytes(value.toString()) 1307 | var len = bytes.length 1308 | for (i = start; i < end; i++) { 1309 | this[i] = bytes[i % len] 1310 | } 1311 | } 1312 | 1313 | return this 1314 | } 1315 | 1316 | // HELPER FUNCTIONS 1317 | // ================ 1318 | 1319 | var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g 1320 | 1321 | function base64clean (str) { 1322 | // Node strips out invalid characters like \n and \t from the string, base64-js does not 1323 | str = stringtrim(str).replace(INVALID_BASE64_RE, '') 1324 | // Node converts strings with length < 2 to '' 1325 | if (str.length < 2) return '' 1326 | // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not 1327 | while (str.length % 4 !== 0) { 1328 | str = str + '=' 1329 | } 1330 | return str 1331 | } 1332 | 1333 | function stringtrim (str) { 1334 | if (str.trim) return str.trim() 1335 | return str.replace(/^\s+|\s+$/g, '') 1336 | } 1337 | 1338 | function toHex (n) { 1339 | if (n < 16) return '0' + n.toString(16) 1340 | return n.toString(16) 1341 | } 1342 | 1343 | function utf8ToBytes (string, units) { 1344 | units = units || Infinity 1345 | var codePoint 1346 | var length = string.length 1347 | var leadSurrogate = null 1348 | var bytes = [] 1349 | 1350 | for (var i = 0; i < length; i++) { 1351 | codePoint = string.charCodeAt(i) 1352 | 1353 | // is surrogate component 1354 | if (codePoint > 0xD7FF && codePoint < 0xE000) { 1355 | // last char was a lead 1356 | if (!leadSurrogate) { 1357 | // no lead yet 1358 | if (codePoint > 0xDBFF) { 1359 | // unexpected trail 1360 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 1361 | continue 1362 | } else if (i + 1 === length) { 1363 | // unpaired lead 1364 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 1365 | continue 1366 | } 1367 | 1368 | // valid lead 1369 | leadSurrogate = codePoint 1370 | 1371 | continue 1372 | } 1373 | 1374 | // 2 leads in a row 1375 | if (codePoint < 0xDC00) { 1376 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 1377 | leadSurrogate = codePoint 1378 | continue 1379 | } 1380 | 1381 | // valid surrogate pair 1382 | codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 1383 | } else if (leadSurrogate) { 1384 | // valid bmp char, but last char was a lead 1385 | if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 1386 | } 1387 | 1388 | leadSurrogate = null 1389 | 1390 | // encode utf8 1391 | if (codePoint < 0x80) { 1392 | if ((units -= 1) < 0) break 1393 | bytes.push(codePoint) 1394 | } else if (codePoint < 0x800) { 1395 | if ((units -= 2) < 0) break 1396 | bytes.push( 1397 | codePoint >> 0x6 | 0xC0, 1398 | codePoint & 0x3F | 0x80 1399 | ) 1400 | } else if (codePoint < 0x10000) { 1401 | if ((units -= 3) < 0) break 1402 | bytes.push( 1403 | codePoint >> 0xC | 0xE0, 1404 | codePoint >> 0x6 & 0x3F | 0x80, 1405 | codePoint & 0x3F | 0x80 1406 | ) 1407 | } else if (codePoint < 0x110000) { 1408 | if ((units -= 4) < 0) break 1409 | bytes.push( 1410 | codePoint >> 0x12 | 0xF0, 1411 | codePoint >> 0xC & 0x3F | 0x80, 1412 | codePoint >> 0x6 & 0x3F | 0x80, 1413 | codePoint & 0x3F | 0x80 1414 | ) 1415 | } else { 1416 | throw new Error('Invalid code point') 1417 | } 1418 | } 1419 | 1420 | return bytes 1421 | } 1422 | 1423 | function asciiToBytes (str) { 1424 | var byteArray = [] 1425 | for (var i = 0; i < str.length; i++) { 1426 | // Node's code seems to be doing this and not & 0x7F.. 1427 | byteArray.push(str.charCodeAt(i) & 0xFF) 1428 | } 1429 | return byteArray 1430 | } 1431 | 1432 | function utf16leToBytes (str, units) { 1433 | var c, hi, lo 1434 | var byteArray = [] 1435 | for (var i = 0; i < str.length; i++) { 1436 | if ((units -= 2) < 0) break 1437 | 1438 | c = str.charCodeAt(i) 1439 | hi = c >> 8 1440 | lo = c % 256 1441 | byteArray.push(lo) 1442 | byteArray.push(hi) 1443 | } 1444 | 1445 | return byteArray 1446 | } 1447 | 1448 | function base64ToBytes (str) { 1449 | return base64.toByteArray(base64clean(str)) 1450 | } 1451 | 1452 | function blitBuffer (src, dst, offset, length) { 1453 | for (var i = 0; i < length; i++) { 1454 | if ((i + offset >= dst.length) || (i >= src.length)) break 1455 | dst[i + offset] = src[i] 1456 | } 1457 | return i 1458 | } 1459 | 1460 | }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) 1461 | },{"base64-js":2,"ieee754":3,"isarray":4}],2:[function(require,module,exports){ 1462 | 'use strict' 1463 | 1464 | exports.toByteArray = toByteArray 1465 | exports.fromByteArray = fromByteArray 1466 | 1467 | var lookup = [] 1468 | var revLookup = [] 1469 | var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array 1470 | 1471 | function init () { 1472 | var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' 1473 | for (var i = 0, len = code.length; i < len; ++i) { 1474 | lookup[i] = code[i] 1475 | revLookup[code.charCodeAt(i)] = i 1476 | } 1477 | 1478 | revLookup['-'.charCodeAt(0)] = 62 1479 | revLookup['_'.charCodeAt(0)] = 63 1480 | } 1481 | 1482 | init() 1483 | 1484 | function toByteArray (b64) { 1485 | var i, j, l, tmp, placeHolders, arr 1486 | var len = b64.length 1487 | 1488 | if (len % 4 > 0) { 1489 | throw new Error('Invalid string. Length must be a multiple of 4') 1490 | } 1491 | 1492 | // the number of equal signs (place holders) 1493 | // if there are two placeholders, than the two characters before it 1494 | // represent one byte 1495 | // if there is only one, then the three characters before it represent 2 bytes 1496 | // this is just a cheap hack to not do indexOf twice 1497 | placeHolders = b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 1498 | 1499 | // base64 is 4/3 + up to two characters of the original data 1500 | arr = new Arr(len * 3 / 4 - placeHolders) 1501 | 1502 | // if there are placeholders, only get up to the last complete 4 chars 1503 | l = placeHolders > 0 ? len - 4 : len 1504 | 1505 | var L = 0 1506 | 1507 | for (i = 0, j = 0; i < l; i += 4, j += 3) { 1508 | tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)] 1509 | arr[L++] = (tmp >> 16) & 0xFF 1510 | arr[L++] = (tmp >> 8) & 0xFF 1511 | arr[L++] = tmp & 0xFF 1512 | } 1513 | 1514 | if (placeHolders === 2) { 1515 | tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4) 1516 | arr[L++] = tmp & 0xFF 1517 | } else if (placeHolders === 1) { 1518 | tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2) 1519 | arr[L++] = (tmp >> 8) & 0xFF 1520 | arr[L++] = tmp & 0xFF 1521 | } 1522 | 1523 | return arr 1524 | } 1525 | 1526 | function tripletToBase64 (num) { 1527 | return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] 1528 | } 1529 | 1530 | function encodeChunk (uint8, start, end) { 1531 | var tmp 1532 | var output = [] 1533 | for (var i = start; i < end; i += 3) { 1534 | tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) 1535 | output.push(tripletToBase64(tmp)) 1536 | } 1537 | return output.join('') 1538 | } 1539 | 1540 | function fromByteArray (uint8) { 1541 | var tmp 1542 | var len = uint8.length 1543 | var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes 1544 | var output = '' 1545 | var parts = [] 1546 | var maxChunkLength = 16383 // must be multiple of 3 1547 | 1548 | // go through the array every three bytes, we'll deal with trailing stuff later 1549 | for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { 1550 | parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))) 1551 | } 1552 | 1553 | // pad the end with zeros, but make sure to not forget the extra bytes 1554 | if (extraBytes === 1) { 1555 | tmp = uint8[len - 1] 1556 | output += lookup[tmp >> 2] 1557 | output += lookup[(tmp << 4) & 0x3F] 1558 | output += '==' 1559 | } else if (extraBytes === 2) { 1560 | tmp = (uint8[len - 2] << 8) + (uint8[len - 1]) 1561 | output += lookup[tmp >> 10] 1562 | output += lookup[(tmp >> 4) & 0x3F] 1563 | output += lookup[(tmp << 2) & 0x3F] 1564 | output += '=' 1565 | } 1566 | 1567 | parts.push(output) 1568 | 1569 | return parts.join('') 1570 | } 1571 | 1572 | },{}],3:[function(require,module,exports){ 1573 | exports.read = function (buffer, offset, isLE, mLen, nBytes) { 1574 | var e, m 1575 | var eLen = nBytes * 8 - mLen - 1 1576 | var eMax = (1 << eLen) - 1 1577 | var eBias = eMax >> 1 1578 | var nBits = -7 1579 | var i = isLE ? (nBytes - 1) : 0 1580 | var d = isLE ? -1 : 1 1581 | var s = buffer[offset + i] 1582 | 1583 | i += d 1584 | 1585 | e = s & ((1 << (-nBits)) - 1) 1586 | s >>= (-nBits) 1587 | nBits += eLen 1588 | for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {} 1589 | 1590 | m = e & ((1 << (-nBits)) - 1) 1591 | e >>= (-nBits) 1592 | nBits += mLen 1593 | for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {} 1594 | 1595 | if (e === 0) { 1596 | e = 1 - eBias 1597 | } else if (e === eMax) { 1598 | return m ? NaN : ((s ? -1 : 1) * Infinity) 1599 | } else { 1600 | m = m + Math.pow(2, mLen) 1601 | e = e - eBias 1602 | } 1603 | return (s ? -1 : 1) * m * Math.pow(2, e - mLen) 1604 | } 1605 | 1606 | exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { 1607 | var e, m, c 1608 | var eLen = nBytes * 8 - mLen - 1 1609 | var eMax = (1 << eLen) - 1 1610 | var eBias = eMax >> 1 1611 | var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) 1612 | var i = isLE ? 0 : (nBytes - 1) 1613 | var d = isLE ? 1 : -1 1614 | var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0 1615 | 1616 | value = Math.abs(value) 1617 | 1618 | if (isNaN(value) || value === Infinity) { 1619 | m = isNaN(value) ? 1 : 0 1620 | e = eMax 1621 | } else { 1622 | e = Math.floor(Math.log(value) / Math.LN2) 1623 | if (value * (c = Math.pow(2, -e)) < 1) { 1624 | e-- 1625 | c *= 2 1626 | } 1627 | if (e + eBias >= 1) { 1628 | value += rt / c 1629 | } else { 1630 | value += rt * Math.pow(2, 1 - eBias) 1631 | } 1632 | if (value * c >= 2) { 1633 | e++ 1634 | c /= 2 1635 | } 1636 | 1637 | if (e + eBias >= eMax) { 1638 | m = 0 1639 | e = eMax 1640 | } else if (e + eBias >= 1) { 1641 | m = (value * c - 1) * Math.pow(2, mLen) 1642 | e = e + eBias 1643 | } else { 1644 | m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) 1645 | e = 0 1646 | } 1647 | } 1648 | 1649 | for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} 1650 | 1651 | e = (e << mLen) | m 1652 | eLen += mLen 1653 | for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} 1654 | 1655 | buffer[offset + i - d] |= s * 128 1656 | } 1657 | 1658 | },{}],4:[function(require,module,exports){ 1659 | var toString = {}.toString; 1660 | 1661 | module.exports = Array.isArray || function (arr) { 1662 | return toString.call(arr) == '[object Array]'; 1663 | }; 1664 | 1665 | },{}]},{},[1])(1) 1666 | }); --------------------------------------------------------------------------------