├── .gitignore ├── Makefile ├── .gitmodules ├── package.json ├── test ├── browser.html ├── test.guessLanguage.js └── assert.js ├── example.html ├── README.md └── lib └── guessLanguage.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store 3 | tmp/* -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | test: 2 | @./node_modules/.bin/mocha -u tdd 3 | 4 | .PHONY: test -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "vendor/mocha"] 2 | path = vendor/mocha 3 | url = git://github.com/visionmedia/mocha.git 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "guessLanguage.js", 3 | "version": "0.1.0", 4 | "description": "Guess the natural language of a run of text", 5 | "keywords": [ 6 | "guesslanguage", 7 | "guess_language" 8 | ], 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/richtr/guessLanguage.js" 12 | }, 13 | "author": "Rich Tibbett ", 14 | "license": "LGPL", 15 | "main": "./lib/guessLanguage", 16 | "engines": { 17 | "node": ">= 0.4" 18 | }, 19 | "scripts": { 20 | "test": "make test" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /test/browser.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | guessLanguage.js test runner 6 | 7 | 8 | 9 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 25 | -------------------------------------------------------------------------------- /example.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | guessLanguage.js library demo 4 | 5 | 21 | 22 |

guessLanguage.js library demo

23 | 24 | 25 | 26 | 27 |

Enter some text below to try this library:

28 | 29 | 30 | 31 |
32 | 33 | 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | guessLanguage.js 2 | ==================== 3 | 4 | A JavaScript language detection library based on trigram statistical analysis. This library works equally well when run in either the Browser or Node.js. 5 | 6 | #### Usage #### 7 | 8 | You can clone this repo as follows: 9 | 10 | git clone git@github.com:richtr/guessLanguage.js.git 11 | 12 | You can use this library in a web page as follows: 13 | 14 | 15 | 16 | 21 | 22 | You can use this library in a Node.js project as follows (npm package coming soon!): 23 | 24 | var guessLanguage = require('./lib/guessLanguage'); 25 | 26 | guessLanguage.detect('...input text here...', function(language) { 27 | console.log('Detected language of provided text is [' + language + ']'); 28 | }); 29 | 30 | #### Feedback #### 31 | 32 | Please note this library is in the early stages of development. If you find any bugs or issues please file them on this project and I'll take a look. 33 | 34 | Please also feel free to catch me on Twitter [@richtibbett](http://twitter.com/richtibbett/). -------------------------------------------------------------------------------- /test/test.guessLanguage.js: -------------------------------------------------------------------------------- 1 | var assert = (typeof assert !== "undefined") ? assert : require('assert'); 2 | var guessLanguage = (typeof guessLanguage !== "undefined") ? guessLanguage : require('./../lib/guessLanguage'); 3 | 4 | var tests = [ 5 | ["This is a test of the language checker.", "en"], 6 | ["Vérifions que le détecteur de langue fonctionne.", "fr"], 7 | ["Sprawdźmy, czy odgadywacz języków pracuje", "pl"], 8 | ["авай проверить узнает ли наш угадатель русски язык", "ru"], 9 | ["La respuesta de los acreedores a la oferta argentina para salir " + 10 | "del default no ha sido muy positiv", "es"], 11 | ["Сайлау нәтижесінде дауыстардың басым бөлігін ел премьер " + 12 | "министрі Виктор Янукович пен оның қарсыласы, оппозиция " + 13 | "жетекшісі Виктор Ющенко алды.", "kk"], 14 | ["милиция ва уч солиқ идораси ходимлари яраланган. Шаҳарда " + 15 | "хавфсизлик чоралари кучайтирилган.", "uz"], 16 | ["көрбөгөндөй элдик толкундоо болуп, Кокон шаарынын көчөлөрүндө " + 17 | "бир нече миң киши нааразылык билдирди.", "ky"], 18 | ["yakın tarihin en çekişmeli başkanlık seçiminde oy verme işlemi " + 19 | "sürerken, katılımda rekor bekleniyor.", "tr"], 20 | ["Daxil olan xəbərlərdə deyilir ki, 6 nəfər Bağdadın mərkəzində " + 21 | "yerləşən Təhsil Nazirliyinin binası yaxınlığında baş vermiş " + 22 | "partlayış zamanı həlak olub.", "az"], 23 | [" ملايين الناخبين الأمريكيين يدلون بأصواتهم وسط إقبال قياسي على " + 24 | "انتخابات هي الأشد تنافسا منذ عقود", "ar"], 25 | ["Американське суспільство, поділене суперечностями, збирається " + 26 | "взяти активну участь у голосуванні", "uk"], 27 | ["Francouzský ministr financí zmírnil výhrady vůči nízkým " + 28 | "firemním daním v nových členských státech EU", "cs"], 29 | ["biće prilično izjednačena, sugerišu najnovije ankete. " + 30 | "Oba kandidata tvrde da su sposobni da dobiju rat protiv " + 31 | "terorizma", "hr"], 32 | [" е готов да даде гаранции, че няма да прави ядрено оръжие, " + 33 | "ако му се разреши мирна атомна програма", "bg"], 34 | ["на јавното мислење покажуваат дека трката е толку тесна, " + 35 | "што се очекува двајцата соперници да ја прекршат традицијата " + 36 | "и да се појават и на самиот изборен ден.", "mk"], 37 | ["în acest sens aparţinînd Adunării Generale a organizaţiei, " + 38 | "în ciuda faptului că mai multe dintre solicitările organizaţiei " + 39 | "privind organizarea scrutinului nu au fost soluţionate", "ro"], 40 | ["kaluan ditën e fundit të fushatës në shtetet kryesore " + 41 | "për të siguruar sa më shumë votues.", "sq"], 42 | ["αναμένεται να σπάσουν παράδοση δεκαετιών και να συνεχίσουν " + 43 | "την εκστρατεία τους ακόμη και τη μέρα των εκλογών", "el"], 44 | [" 美国各州选民今天开始正式投票。据信,", "zh"], 45 | [" Die kritiek was volgens hem bitter hard nodig, " + 46 | "omdat Nederland binnen een paar jaar in een soort Belfast zou " + 47 | "dreigen te veranderen", "nl"], 48 | ["På denne side bringer vi billeder fra de mange forskellige " + 49 | "forberedelser til arrangementet, efterhånden som vi får dem ", 50 | "da"], 51 | ["Vi säger att Frälsningen är en gåva till alla, fritt och för " + 52 | "intet. Men som vi nämnt så finns det två villkor som måste", 53 | "sv"], 54 | ["Nominasjonskomiteen i Akershus KrF har skviset ut Einar Holstad " + 55 | "fra stortingslisten. Ytre Enebakk-mannen har plass p Stortinget " + 56 | "s lenge Valgerd Svarstad Haugland sitter i", "nb"], 57 | ["on julkishallinnon verkkopalveluiden yhteinen osoite. " + 58 | "Kansalaisten arkielämää helpottavaa tietoa on koottu eri " + 59 | "aihealueisiin", "fi"], 60 | ["Ennetamaks reisil ebameeldivaid vahejuhtumeid vii end kurssi " + 61 | "reisidokumentide ja viisade reeglitega ning muu praktilise " + 62 | "informatsiooniga", "et"], 63 | ["Hiába jön létre az önkéntes magyar haderő, hiába nem lesz " + 64 | "többé bevonulás, változatlanul fennmarad a hadkötelezettség " + 65 | "intézménye", "hu"], 66 | ["հարաբերական", "hy"], 67 | ["Hai vấn đề khó chịu với màn hình thường gặp nhất khi bạn dùng " + 68 | "laptop là vết trầy xước và điểm chết. Sau đây là vài cách xử " + 69 | "lý chúng.", "vi"], 70 | ["トヨタ自動車、フィリピンの植林活動で第三者認証取得 " + 71 | "トヨタ自動車[株](以下、トヨタ)は、2007年9月よりフィリピンのルソン" + 72 | "島北部に位置するカガヤン州ペニャブランカ町", "ja"], 73 | ["", "unknown"], 74 | ]; 75 | 76 | function IdTest(obj) { 77 | describe('Guess Language ID [' + obj[1] + ']', function() { 78 | it('Language is [' + obj[1] + ']', function(done) { 79 | guessLanguage.detect(obj[0], function(id) { 80 | assert.equal(obj[1], id); 81 | done(); 82 | }); 83 | }); 84 | }); 85 | } 86 | 87 | for(var ii = 0, l = tests.length; ii < l; ii++) { 88 | 89 | new IdTest(tests[ii]); 90 | 91 | } 92 | 93 | var text = "Vérifions que le détecteur de langue fonctionne." 94 | 95 | describe('Guess Language Name', function() { 96 | 97 | it('Test Guess Language Name', function(done) { 98 | guessLanguage.name(text, function(name) { 99 | assert.equal("French", name); 100 | done(); 101 | }); 102 | }); 103 | 104 | }); 105 | 106 | describe('Guess Language Code', function() { 107 | 108 | it('Test Guess Language Code', function(done) { 109 | guessLanguage.code(text, function(code) { 110 | assert.equal(26150, code); 111 | done(); 112 | }); 113 | }); 114 | 115 | }); 116 | 117 | describe('Guess Language Info', function() { 118 | 119 | it('Test Guess Language Info', function(done) { 120 | guessLanguage.info(text, function(info) { 121 | assert.deepEqual(["fr", 26150, "French"], info); 122 | done(); 123 | }); 124 | }); 125 | 126 | }); -------------------------------------------------------------------------------- /lib/guessLanguage.js: -------------------------------------------------------------------------------- 1 | /* Guess the natural language of a text 2 | * Copyright (c) 2012, Rich Tibbett 3 | * http://github.com/richtr/guessLanguage.js/ 4 | * 5 | * Original Python package: 6 | * Copyright (c) 2008, Kent S Johnson 7 | * http://code.google.com/p/guess-language/ 8 | * 9 | * Original C++ version for KDE: 10 | * Copyright (c) 2006 Jacob R Rideout 11 | * http://websvn.kde.org/branches/work/sonnet-refactoring/common/nlp/guesslanguage.cpp?view=markup 12 | * 13 | * Original Language::Guess Perl module: 14 | * Copyright (c) 2004-2006 Maciej Ceglowski 15 | * http://web.archive.org/web/20090228163219/http://languid.cantbedone.org/ 16 | * 17 | * Note: Language::Guess is GPL-licensed. KDE developers received permission 18 | * from the author to distribute their port under LGPL: 19 | * http://lists.kde.org/?l=kde-sonnet&m=116910092228811&w=2 20 | * 21 | * This program is free software: you can redistribute it and/or modify it 22 | * under the terms of the GNU Lesser General Public License as published 23 | * by the Free Software Foundation, either version 3 of the License, 24 | * or (at your option) any later version. 25 | * 26 | * This program is distributed in the hope that it will be useful, 27 | * but WITHOUT ANY WARRANTY; without even the implied warranty 28 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 29 | * See the GNU Lesser General Public License for more details. 30 | * 31 | * You should have received a copy of the GNU Lesser General Public License 32 | * along with this program. If not, see . 33 | */ 34 | 35 | (function(global, undefined) { 36 | 37 | var guessLanguage = function() { 38 | 39 | var models = global._languageData || {}; 40 | 41 | if (typeof module === "object" && module.exports === global) { 42 | models = require('./_languageData') || {}; 43 | } 44 | 45 | var MAX_LENGTH = 4096; 46 | var MIN_LENGTH = 20; 47 | var MAX_GRAMS = 300; 48 | 49 | var NAME_MAP = { 50 | "ab": "Abkhazian", 51 | "af": "Afrikaans", 52 | "ar": "Arabic", 53 | "az": "Azeri", 54 | "be": "Belarusian", 55 | "bg": "Bulgarian", 56 | "bn": "Bengali", 57 | "bo": "Tibetan", 58 | "br": "Breton", 59 | "ca": "Catalan", 60 | "ceb": "Cebuano", 61 | "cs": "Czech", 62 | "cy": "Welsh", 63 | "da": "Danish", 64 | "de": "German", 65 | "el": "Greek", 66 | "en": "English", 67 | "eo": "Esperanto", 68 | "es": "Spanish", 69 | "et": "Estonian", 70 | "eu": "Basque", 71 | "fa": "Farsi", 72 | "fi": "Finnish", 73 | "fo": "Faroese", 74 | "fr": "French", 75 | "fy": "Frisian", 76 | "gd": "Scots Gaelic", 77 | "gl": "Galician", 78 | "gu": "Gujarati", 79 | "ha": "Hausa", 80 | "haw": "Hawaiian", 81 | "he": "Hebrew", 82 | "hi": "Hindi", 83 | "hr": "Croatian", 84 | "hu": "Hungarian", 85 | "hy": "Armenian", 86 | "id": "Indonesian", 87 | "is": "Icelandic", 88 | "it": "Italian", 89 | "ja": "Japanese", 90 | "ka": "Georgian", 91 | "kk": "Kazakh", 92 | "km": "Cambodian", 93 | "ko": "Korean", 94 | "ku": "Kurdish", 95 | "ky": "Kyrgyz", 96 | "la": "Latin", 97 | "lt": "Lithuanian", 98 | "lv": "Latvian", 99 | "mg": "Malagasy", 100 | "mk": "Macedonian", 101 | "ml": "Malayalam", 102 | "mn": "Mongolian", 103 | "mr": "Marathi", 104 | "ms": "Malay", 105 | "nd": "Ndebele", 106 | "ne": "Nepali", 107 | "nl": "Dutch", 108 | "nn": "Nynorsk", 109 | "no": "Norwegian", 110 | "nso": "Sepedi", 111 | "pa": "Punjabi", 112 | "pl": "Polish", 113 | "ps": "Pashto", 114 | "pt": "Portuguese", 115 | "pt_PT": "Portuguese (Portugal)", 116 | "pt_BR": "Portuguese (Brazil)", 117 | "ro": "Romanian", 118 | "ru": "Russian", 119 | "sa": "Sanskrit", 120 | "sh": "Serbo-Croatian", 121 | "sk": "Slovak", 122 | "sl": "Slovene", 123 | "so": "Somali", 124 | "sq": "Albanian", 125 | "sr": "Serbian", 126 | "sv": "Swedish", 127 | "sw": "Swahili", 128 | "ta": "Tamil", 129 | "te": "Telugu", 130 | "th": "Thai", 131 | "tl": "Tagalog", 132 | "tlh": "Klingon", 133 | "tn": "Setswana", 134 | "tr": "Turkish", 135 | "ts": "Tsonga", 136 | "tw": "Twi", 137 | "uk": "Ukrainian", 138 | "ur": "Urdu", 139 | "uz": "Uzbek", 140 | "ve": "Venda", 141 | "vi": "Vietnamese", 142 | "xh": "Xhosa", 143 | "zh": "Chinese", 144 | "zh_TW": "Traditional Chinese (Taiwan)", 145 | "zu": "Zulu", 146 | }; 147 | 148 | var IANA_MAP = { 149 | "ab": 12026, 150 | "af": 40, 151 | "ar": 26020, 152 | "az": 26030, 153 | "be": 11890, 154 | "bg": 26050, 155 | "bn": 26040, 156 | "bo": 26601, 157 | "br": 1361, 158 | "ca": 3, 159 | "ceb": 26060, 160 | "cs": 26080, 161 | "cy": 26560, 162 | "da": 26090, 163 | "de": 26160, 164 | "el": 26165, 165 | "en": 26110, 166 | "eo": 11933, 167 | "es": 26460, 168 | "et": 26120, 169 | "eu": 1232, 170 | "fa": 26130, 171 | "fi": 26140, 172 | "fo": 11817, 173 | "fr": 26150, 174 | "fy": 1353, 175 | "gd": 65555, 176 | "gl": 1252, 177 | "gu": 26599, 178 | "ha": 26170, 179 | "haw": 26180, 180 | "he": 26592, 181 | "hi": 26190, 182 | "hr": 26070, 183 | "hu": 26200, 184 | "hy": 26597, 185 | "id": 26220, 186 | "is": 26210, 187 | "it": 26230, 188 | "ja": 26235, 189 | "ka": 26600, 190 | "kk": 26240, 191 | "km": 1222, 192 | "ko": 26255, 193 | "ku": 11815, 194 | "ky": 26260, 195 | "la": 26280, 196 | "lt": 26300, 197 | "lv": 26290, 198 | "mg": 1362, 199 | "mk": 26310, 200 | "ml": 26598, 201 | "mn": 26320, 202 | "mr": 1201, 203 | "ms": 1147, 204 | "ne": 26330, 205 | "nl": 26100, 206 | "nn": 172, 207 | "no": 26340, 208 | "pa": 65550, 209 | "pl": 26380, 210 | "ps": 26350, 211 | "pt": 26390, 212 | "ro": 26400, 213 | "ru": 26410, 214 | "sa": 1500, 215 | "sh": 1399, 216 | "sk": 26430, 217 | "sl": 26440, 218 | "so": 26450, 219 | "sq": 26010, 220 | "sr": 26420, 221 | "sv": 26480, 222 | "sw": 26470, 223 | "ta": 26595, 224 | "te": 26596, 225 | "th": 26594, 226 | "tl": 26490, 227 | "tlh": 26250, 228 | "tn": 65578, 229 | "tr": 26500, 230 | "tw": 1499, 231 | "uk": 26520, 232 | "ur": 26530, 233 | "uz": 26540, 234 | "vi": 26550, 235 | "zh": 26065, 236 | "zh_TW": 22, 237 | }; 238 | 239 | var SINGLETONS = [ 240 | ["Armenian", "hy"], 241 | ["Hebrew", "he"], 242 | ["Bengali", "bn"], 243 | ["Gurmukhi", "pa"], 244 | ["Greek", "el"], 245 | ["Gujarati", "gu"], 246 | ["Oriya", "or"], 247 | ["Tamil", "ta"], 248 | ["Telugu", "te"], 249 | ["Kannada", "kn"], 250 | ["Malayalam", "ml"], 251 | ["Sinhala", "si"], 252 | ["Thai", "th"], 253 | ["Lao", "lo"], 254 | ["Tibetan", "bo"], 255 | ["Burmese", "my"], 256 | ["Georgian", "ka"], 257 | ["Mongolian", "mn"], 258 | ["Khmer", "km"] 259 | ]; 260 | 261 | var UNKNOWN = 'unknown'; 262 | 263 | var BASIC_LATIN = ["en", "ceb", "ha", "so", "tlh", "id", "haw", "la", "sw", "eu", "nr", "nso", "zu", "xh", "ss", "st", "tn", "ts"]; 264 | var EXTENDED_LATIN = ["cs", "af", "pl", "hr", "ro", "sk", "sl", "tr", "hu", "az", "et", "sq", "ca", "es", "fr", "de", "nl", "it", "da", "is", "no", "sv", "fi", "lv", "pt", "ve", "lt", "tl", "cy", "vi"]; 265 | var ALL_LATIN = BASIC_LATIN.concat(EXTENDED_LATIN); 266 | var CYRILLIC = ["ru", "uk", "kk", "uz", "mn", "sr", "mk", "bg", "ky"]; 267 | var ARABIC = ["ar", "fa", "ps", "ur"]; 268 | var DEVANAGARI = ["hi", "ne"]; 269 | var PT = ["pt_BR", "pt_PT"]; 270 | 271 | // Unicode char block ranges 272 | var unicodeBlockRanges = [ 273 | ["0000-007F", "Basic Latin"], 274 | ["0080-00FF", "Latin-1 Supplement"], 275 | ["0100-017F", "Latin Extended-A"], 276 | ["0180-024F", "Latin Extended-B"], 277 | ["0250-02AF", "IPA Extensions"], 278 | ["02B0-02FF", "Spacing Modifier Letters"], 279 | ["0300-036F", "Combining Diacritical Marks"], 280 | ["0370-03FF", "Greek and Coptic"], 281 | ["0400-04FF", "Cyrillic"], 282 | ["0500-052F", "Cyrillic Supplement"], 283 | ["0530-058F", "Armenian"], 284 | ["0590-05FF", "Hebrew"], 285 | ["0600-06FF", "Arabic"], 286 | ["0700-074F", "Syriac"], 287 | ["0750-077F", "Arabic Supplement"], 288 | ["0780-07BF", "Thaana"], 289 | ["07C0-07FF", "NKo"], 290 | ["0900-097F", "Devanagari"], 291 | ["0980-09FF", "Bengali"], 292 | ["0A00-0A7F", "Gurmukhi"], 293 | ["0A80-0AFF", "Gujarati"], 294 | ["0B00-0B7F", "Oriya"], 295 | ["0B80-0BFF", "Tamil"], 296 | ["0C00-0C7F", "Telugu"], 297 | ["0C80-0CFF", "Kannada"], 298 | ["0D00-0D7F", "Malayalam"], 299 | ["0D80-0DFF", "Sinhala"], 300 | ["0E00-0E7F", "Thai"], 301 | ["0E80-0EFF", "Lao"], 302 | ["0F00-0FFF", "Tibetan"], 303 | ["1000-109F", "Myanmar"], 304 | ["10A0-10FF", "Georgian"], 305 | ["1100-11FF", "Hangul Jamo"], 306 | ["1200-137F", "Ethiopic"], 307 | ["1380-139F", "Ethiopic Supplement"], 308 | ["13A0-13FF", "Cherokee"], 309 | ["1400-167F", "Unified Canadian Aboriginal Syllabics"], 310 | ["1680-169F", "Ogham"], 311 | ["16A0-16FF", "Runic"], 312 | ["1700-171F", "Tagalog"], 313 | ["1720-173F", "Hanunoo"], 314 | ["1740-175F", "Buhid"], 315 | ["1760-177F", "Tagbanwa"], 316 | ["1780-17FF", "Khmer"], 317 | ["1800-18AF", "Mongolian"], 318 | ["1900-194F", "Limbu"], 319 | ["1950-197F", "Tai Le"], 320 | ["1980-19DF", "New Tai Lue"], 321 | ["19E0-19FF", "Khmer Symbols"], 322 | ["1A00-1A1F", "Buginese"], 323 | ["1B00-1B7F", "Balinese"], 324 | ["1D00-1D7F", "Phonetic Extensions"], 325 | ["1D80-1DBF", "Phonetic Extensions Supplement"], 326 | ["1DC0-1DFF", "Combining Diacritical Marks Supplement"], 327 | ["1E00-1EFF", "Latin Extended Additional"], 328 | ["1F00-1FFF", "Greek Extended"], 329 | ["2000-206F", "General Punctuation"], 330 | ["2070-209F", "Superscripts and Subscripts"], 331 | ["20A0-20CF", "Currency Symbols"], 332 | ["20D0-20FF", "Combining Diacritical Marks for Symbols"], 333 | ["2100-214F", "Letterlike Symbols"], 334 | ["2150-218F", "Number Forms"], 335 | ["2190-21FF", "Arrows"], 336 | ["2200-22FF", "Mathematical Operators"], 337 | ["2300-23FF", "Miscellaneous Technical"], 338 | ["2400-243F", "Control Pictures"], 339 | ["2440-245F", "Optical Character Recognition"], 340 | ["2460-24FF", "Enclosed Alphanumerics"], 341 | ["2500-257F", "Box Drawing"], 342 | ["2580-259F", "Block Elements"], 343 | ["25A0-25FF", "Geometric Shapes"], 344 | ["2600-26FF", "Miscellaneous Symbols"], 345 | ["2700-27BF", "Dingbats"], 346 | ["27C0-27EF", "Miscellaneous Mathematical Symbols-A"], 347 | ["27F0-27FF", "Supplemental Arrows-A"], 348 | ["2800-28FF", "Braille Patterns"], 349 | ["2900-297F", "Supplemental Arrows-B"], 350 | ["2980-29FF", "Miscellaneous Mathematical Symbols-B"], 351 | ["2A00-2AFF", "Supplemental Mathematical Operators"], 352 | ["2B00-2BFF", "Miscellaneous Symbols and Arrows"], 353 | ["2C00-2C5F", "Glagolitic"], 354 | ["2C60-2C7F", "Latin Extended-C"], 355 | ["2C80-2CFF", "Coptic"], 356 | ["2D00-2D2F", "Georgian Supplement"], 357 | ["2D30-2D7F", "Tifinagh"], 358 | ["2D80-2DDF", "Ethiopic Extended"], 359 | ["2E00-2E7F", "Supplemental Punctuation"], 360 | ["2E80-2EFF", "CJK Radicals Supplement"], 361 | ["2F00-2FDF", "KangXi Radicals"], 362 | ["2FF0-2FFF", "Ideographic Description Characters"], 363 | ["3000-303F", "CJK Symbols and Punctuation"], 364 | ["3040-309F", "Hiragana"], 365 | ["30A0-30FF", "Katakana"], 366 | ["3100-312F", "Bopomofo"], 367 | ["3130-318F", "Hangul Compatibility Jamo"], 368 | ["3190-319F", "Kanbun"], 369 | ["31A0-31BF", "Bopomofo Extended"], 370 | ["31C0-31EF", "CJK Strokes"], 371 | ["31F0-31FF", "Katakana Phonetic Extensions"], 372 | ["3200-32FF", "Enclosed CJK Letters and Months"], 373 | ["3300-33FF", "CJK Compatibility"], 374 | ["3400-4DBF", "CJK Unified Ideographs Extension A"], 375 | ["4DC0-4DFF", "Yijing Hexagram Symbols"], 376 | ["4E00-9FFF", "CJK Unified Ideographs"], 377 | ["A000-A48F", "Yi Syllables"], 378 | ["A490-A4CF", "Yi Radicals"], 379 | ["A700-A71F", "Modifier Tone Letters"], 380 | ["A720-A7FF", "Latin Extended-D"], 381 | ["A800-A82F", "Syloti Nagri"], 382 | ["A840-A87F", "Phags-pa"], 383 | ["AC00-D7AF", "Hangul Syllables"], 384 | ["D800-DB7F", "High Surrogates"], 385 | ["DB80-DBFF", "High Private Use Surrogates"], 386 | ["DC00-DFFF", "Low Surrogates"], 387 | ["E000-F8FF", "Private Use Area"], 388 | ["F900-FAFF", "CJK Compatibility Ideographs"], 389 | ["FB00-FB4F", "Alphabetic Presentation Forms"], 390 | ["FB50-FDFF", "Arabic Presentation Forms-A"], 391 | ["FE00-FE0F", "Variation Selectors"], 392 | ["FE10-FE1F", "Vertical Forms"], 393 | ["FE20-FE2F", "Combining Half Marks"], 394 | ["FE30-FE4F", "CJK Compatibility Forms"], 395 | ["FE50-FE6F", "Small Form Variants"], 396 | ["FE70-FEFF", "Arabic Presentation Forms-B"], 397 | ["FF00-FFEF", "Halfwidth and Fullwidth Forms"], 398 | ["FFF0-FFFF", "Specials"], 399 | /*["10000-1007F", "Linear B Syllabary"], 400 | ["10080-100FF", "Linear B Ideograms"], 401 | ["10100-1013F", "Aegean Numbers"], 402 | ["10140-1018F", "Ancient Greek Numbers"], 403 | ["10300-1032F", "Old Italic"], 404 | ["10330-1034F", "Gothic"], 405 | ["10380-1039F", "Ugaritic"], 406 | ["103A0-103DF", "Old Persian"], 407 | ["10400-1044F", "Deseret"], 408 | ["10450-1047F", "Shavian"], 409 | ["10480-104AF", "Osmanya"], 410 | ["10800-1083F", "Cypriot Syllabary"], 411 | ["10900-1091F", "Phoenician"], 412 | ["10A00-10A5F", "Kharoshthi"], 413 | ["12000-123FF", "Cuneiform"], 414 | ["12400-1247F", "Cuneiform Numbers and Punctuation"], 415 | ["1D000-1D0FF", "Byzantine Musical Symbols"], 416 | ["1D100-1D1FF", "Musical Symbols"], 417 | ["1D200-1D24F", "Ancient Greek Musical Notation"], 418 | ["1D300-1D35F", "Tai Xuan Jing Symbols"], 419 | ["1D360-1D37F", "Counting Rod Numerals"], 420 | ["1D400-1D7FF", "Mathematical Alphanumeric Symbols"], 421 | ["20000-2A6DF", "CJK Unified Ideographs Extension B"], 422 | ["2F800-2FA1F", "CJK Compatibility Ideographs Supplement"], 423 | ["E0000-E007F", "Tags"], 424 | ["E0100-E01EF", "Variation Selectors Supplement"], 425 | ["F0000-FFFFF", "Supplementary Private Use Area-A"], 426 | ["100000-10FFFF", "Supplementary Private Use Area-B"]*/ 427 | ]; 428 | 429 | // Unicode char greedy regex object matchers 430 | var unicodeBlockTests = compileUnicodeBlockTests(); 431 | 432 | function compileUnicodeBlockTests() { 433 | var obj = {}; 434 | for (var i = 0, l = unicodeBlockRanges.length; i < l; i++) { 435 | var x = unicodeBlockRanges[i][0].split('-'); 436 | x[0] = '\\u' + x[0]; 437 | x[1] = '\\u' + x[1]; 438 | obj[unicodeBlockRanges[i][1]] = new RegExp('[' + x[0] + '-' + x[1] + ']', 'g'); 439 | } 440 | return obj; 441 | } 442 | 443 | function findRuns(text) { 444 | 445 | var relevant_runs = {}; 446 | 447 | var unicodeBlockNames = Object.keys(unicodeBlockTests); 448 | 449 | for (var i = 0, l = unicodeBlockNames.length; i < l; i++) { 450 | 451 | // Count the number of characters in each character block. 452 | var charCount = text.match(unicodeBlockTests[unicodeBlockNames[i]]); 453 | 454 | // return run types that used for 40% or more of the string 455 | // always return basic latin if found more than 15% 456 | // and extended additional latin if over 10% (for Vietnamese) 457 | var pct = (charCount ? charCount.length : 0) / text.length; 458 | 459 | relevant_runs[unicodeBlockNames[i]] = pct; 460 | 461 | } 462 | 463 | return relevant_runs; 464 | } 465 | 466 | function identify(text, callback) { 467 | 468 | var scripts = findRuns(text); 469 | 470 | // Identify the language. 471 | if (scripts["Hangul Syllables"] + scripts["Hangul Jamo"] + scripts["Hangul Compatibility Jamo"] >= 0.4) { 472 | callback.apply(undefined, ["ko"]); 473 | return; 474 | } 475 | 476 | if (scripts["Greek and Coptic"] >= 0.4) { 477 | callback.apply(undefined, ["el"]); 478 | return; 479 | } 480 | 481 | if (scripts["Hiragana"] + scripts["Katakana"] + scripts["Katakana Phonetic Extensions"] >= 0.2) { 482 | callback.apply(undefined, ["ja"]); 483 | return; 484 | } 485 | 486 | if (scripts["CJK Unified Ideographs"] + scripts["Bopomofo"] + scripts["Bopomofo Extended"] + scripts["KangXi Radicals"] >= 0.4) { 487 | callback.apply(undefined, ["zh"]); 488 | return; 489 | } 490 | 491 | if (scripts["Cyrillic"] >= 0.4) { 492 | check(text, CYRILLIC, callback); 493 | return; 494 | } 495 | 496 | if (scripts["Arabic"] + scripts["Arabic Presentation Forms-A"] + scripts["Arabic Presentation Forms-B"] >= 0.4) { 497 | check(text, ARABIC, callback); 498 | return; 499 | } 500 | 501 | if (scripts["Devanagari"] >= 0.4) { 502 | check(text, DEVANAGARI, callback); 503 | return; 504 | } 505 | 506 | // Try languages with unique scripts 507 | for (var i = 0, l = SINGLETONS.length; i < l; i++) { 508 | if (scripts[SINGLETONS[i][0]] >= 0.4) { 509 | callback.apply(undefined, [SINGLETONS[i][1]]); 510 | return; 511 | } 512 | } 513 | 514 | // Extended Latin 515 | if (scripts["Latin-1 Supplement"] + scripts["Latin Extended-A"] + scripts["IPA Extensions"] >= 0.4) { 516 | check(text, EXTENDED_LATIN, function(latin_lang) { 517 | if (latin_lang == "pt") { 518 | check(text, PT, callback); 519 | } else { 520 | callback.apply(undefined, [latin_lang]); 521 | } 522 | }); 523 | return; 524 | } 525 | 526 | if (scripts["Basic Latin"] >= 0.15) { 527 | check(text, ALL_LATIN, callback); 528 | return; 529 | } 530 | 531 | callback.apply(undefined, [UNKNOWN]); 532 | // return; 533 | } 534 | 535 | function check(sample, langs, callback) { 536 | 537 | if (sample.length < MIN_LENGTH) { 538 | callback.apply(undefined, [UNKNOWN]); 539 | return; 540 | } 541 | 542 | var scores = {}; 543 | var model = createOrderedModel(sample) 544 | for (var i = 0, l = langs.length; i < l; i++) { 545 | 546 | var lkey = langs[i].toLowerCase(); 547 | 548 | var known_model = models[lkey] || null; 549 | 550 | if (!known_model) { 551 | continue; 552 | } 553 | 554 | scores[lkey] = distance(model, known_model); 555 | 556 | } 557 | 558 | var scoresArr = []; 559 | for (var index in scores) { 560 | scoresArr.push([index, scores[index]]); 561 | } 562 | 563 | if (scoresArr.length == 0) { 564 | callback.apply(undefined, [UNKNOWN]); 565 | return; 566 | } 567 | 568 | // we want the lowest score, less distance = greater chance of match 569 | var sortedScores = scoresArr.sort(function(objA, objB) { 570 | return objA[1] - objB[1]; // sort low-to-high 571 | }); 572 | 573 | // return the best match we've now calculated 574 | callback.apply(undefined, [sortedScores[0][0]]); 575 | //return; 576 | } 577 | 578 | function createOrderedModel(content) { 579 | // Create a list of trigrams in content sorted by frequency. 580 | var trigrams = {}, 581 | sortedTrigrams = []; 582 | var content = content.toLowerCase(); 583 | 584 | var contentArr = content.split(""); 585 | for (var i = 0, l = contentArr.length - 2; i < l; i++) { 586 | var trigramKey = contentArr[i] + contentArr[i + 1] + contentArr[i + 2] + ""; 587 | if (!trigrams[trigramKey]) { 588 | trigrams[trigramKey] = 1; 589 | } else { 590 | trigrams[trigramKey] += 1; 591 | } 592 | } 593 | 594 | // convert object to array 595 | for (var i in trigrams) { 596 | sortedTrigrams[sortedTrigrams.length] = [i, trigrams[i]]; 597 | } 598 | 599 | // sort array results 600 | return sortedTrigrams.sort(function(objA, objB) { 601 | return objB[1] - objA[1]; // sort high-to-low 602 | }); 603 | } 604 | 605 | function distance(model, known_model) { 606 | // Calculate the distance to the known model. 607 | var dist = 0; 608 | 609 | var keys = Object.keys(model); 610 | 611 | for (var i = 0, l = model.length; i < l; i++) { 612 | 613 | if (known_model[model[i][0]]) { 614 | 615 | dist += Math.abs(model[i][1] - known_model[model[i][0]]); 616 | 617 | } else { 618 | 619 | dist += MAX_GRAMS; 620 | 621 | } 622 | 623 | } 624 | 625 | return dist; 626 | } 627 | 628 | return { 629 | detect: function(text, callback) { 630 | // Return the ISO 639-2 language identifier, i.e. 'en'. 631 | 632 | if (!text) { 633 | callback.apply(undefined, [UNKNOWN]); 634 | return; 635 | } 636 | 637 | text = text.substr(0, MAX_LENGTH).replace(/[\u0021-\u0040]/g, ''); 638 | 639 | identify(text, callback); 640 | 641 | }, 642 | info: function(text, callback) { 643 | // Return language info tuple (id, code, name), i.e. ('en', 26110, 'English'). 644 | 645 | this.detect(text, function(language) { 646 | 647 | if (language === UNKNOWN) { 648 | callback.apply(undefined, [[ UNKNOWN, UNKNOWN, UNKNOWN ]]);; 649 | return; 650 | } 651 | 652 | callback.apply(undefined, [ 653 | 654 | [ language, IANA_MAP[language], NAME_MAP[language] ] 655 | 656 | ]);; 657 | 658 | }); 659 | 660 | }, 661 | code: function(text, callback) { 662 | // Return the language IANA code, i.e. 26110. 663 | 664 | this.detect(text, function(language) { 665 | 666 | if (language === UNKNOWN) { 667 | callback.apply(undefined, [ -1 ]);; 668 | return; 669 | } 670 | 671 | callback.apply(undefined, [ 672 | 673 | IANA_MAP[language] 674 | 675 | ]); 676 | 677 | }); 678 | 679 | }, 680 | name: function(text, callback) { 681 | // Return the full language name, i.e. 'English'. 682 | 683 | this.detect(text, function(language) { 684 | 685 | if (language === UNKNOWN) { 686 | callback.apply(undefined, [ UNKNOWN ]);; 687 | return; 688 | } 689 | 690 | callback.apply(undefined, [ 691 | 692 | NAME_MAP[language] 693 | 694 | ]); 695 | 696 | }); 697 | 698 | } 699 | }; 700 | 701 | }; 702 | 703 | global.guessLanguage = (global.module || {}).exports = new guessLanguage(); 704 | 705 | })(this); 706 | -------------------------------------------------------------------------------- /test/assert.js: -------------------------------------------------------------------------------- 1 | (function(){var require = function (file, cwd) { 2 | var resolved = require.resolve(file, cwd || '/'); 3 | var mod = require.modules[resolved]; 4 | if (!mod) throw new Error( 5 | 'Failed to resolve module ' + file + ', tried ' + resolved 6 | ); 7 | var cached = require.cache[resolved]; 8 | var res = cached? cached.exports : mod(); 9 | return res; 10 | }; 11 | 12 | require.paths = []; 13 | require.modules = {}; 14 | require.cache = {}; 15 | require.extensions = [".js",".coffee"]; 16 | 17 | require._core = { 18 | 'assert': true, 19 | 'events': true, 20 | 'fs': true, 21 | 'path': true, 22 | 'vm': true 23 | }; 24 | 25 | require.resolve = (function () { 26 | return function (x, cwd) { 27 | if (!cwd) cwd = '/'; 28 | 29 | if (require._core[x]) return x; 30 | var path = require.modules.path(); 31 | cwd = path.resolve('/', cwd); 32 | var y = cwd || '/'; 33 | 34 | if (x.match(/^(?:\.\.?\/|\/)/)) { 35 | var m = loadAsFileSync(path.resolve(y, x)) 36 | || loadAsDirectorySync(path.resolve(y, x)); 37 | if (m) return m; 38 | } 39 | 40 | var n = loadNodeModulesSync(x, y); 41 | if (n) return n; 42 | 43 | throw new Error("Cannot find module '" + x + "'"); 44 | 45 | function loadAsFileSync (x) { 46 | x = path.normalize(x); 47 | if (require.modules[x]) { 48 | return x; 49 | } 50 | 51 | for (var i = 0; i < require.extensions.length; i++) { 52 | var ext = require.extensions[i]; 53 | if (require.modules[x + ext]) return x + ext; 54 | } 55 | } 56 | 57 | function loadAsDirectorySync (x) { 58 | x = x.replace(/\/+$/, ''); 59 | var pkgfile = path.normalize(x + '/package.json'); 60 | if (require.modules[pkgfile]) { 61 | var pkg = require.modules[pkgfile](); 62 | var b = pkg.browserify; 63 | if (typeof b === 'object' && b.main) { 64 | var m = loadAsFileSync(path.resolve(x, b.main)); 65 | if (m) return m; 66 | } 67 | else if (typeof b === 'string') { 68 | var m = loadAsFileSync(path.resolve(x, b)); 69 | if (m) return m; 70 | } 71 | else if (pkg.main) { 72 | var m = loadAsFileSync(path.resolve(x, pkg.main)); 73 | if (m) return m; 74 | } 75 | } 76 | 77 | return loadAsFileSync(x + '/index'); 78 | } 79 | 80 | function loadNodeModulesSync (x, start) { 81 | var dirs = nodeModulesPathsSync(start); 82 | for (var i = 0; i < dirs.length; i++) { 83 | var dir = dirs[i]; 84 | var m = loadAsFileSync(dir + '/' + x); 85 | if (m) return m; 86 | var n = loadAsDirectorySync(dir + '/' + x); 87 | if (n) return n; 88 | } 89 | 90 | var m = loadAsFileSync(x); 91 | if (m) return m; 92 | } 93 | 94 | function nodeModulesPathsSync (start) { 95 | var parts; 96 | if (start === '/') parts = [ '' ]; 97 | else parts = path.normalize(start).split('/'); 98 | 99 | var dirs = []; 100 | for (var i = parts.length - 1; i >= 0; i--) { 101 | if (parts[i] === 'node_modules') continue; 102 | var dir = parts.slice(0, i + 1).join('/') + '/node_modules'; 103 | dirs.push(dir); 104 | } 105 | 106 | return dirs; 107 | } 108 | }; 109 | })(); 110 | 111 | require.alias = function (from, to) { 112 | var path = require.modules.path(); 113 | var res = null; 114 | try { 115 | res = require.resolve(from + '/package.json', '/'); 116 | } 117 | catch (err) { 118 | res = require.resolve(from, '/'); 119 | } 120 | var basedir = path.dirname(res); 121 | 122 | var keys = (Object.keys || function (obj) { 123 | var res = []; 124 | for (var key in obj) res.push(key); 125 | return res; 126 | })(require.modules); 127 | 128 | for (var i = 0; i < keys.length; i++) { 129 | var key = keys[i]; 130 | if (key.slice(0, basedir.length + 1) === basedir + '/') { 131 | var f = key.slice(basedir.length); 132 | require.modules[to + f] = require.modules[basedir + f]; 133 | } 134 | else if (key === basedir) { 135 | require.modules[to] = require.modules[basedir]; 136 | } 137 | } 138 | }; 139 | 140 | (function () { 141 | var process = {}; 142 | 143 | require.define = function (filename, fn) { 144 | if (require.modules.__browserify_process) { 145 | process = require.modules.__browserify_process(); 146 | } 147 | 148 | var dirname = require._core[filename] 149 | ? '' 150 | : require.modules.path().dirname(filename) 151 | ; 152 | 153 | var require_ = function (file) { 154 | var requiredModule = require(file, dirname); 155 | var cached = require.cache[require.resolve(file, dirname)]; 156 | 157 | if (cached && cached.parent === null) { 158 | cached.parent = module_; 159 | } 160 | 161 | return requiredModule; 162 | }; 163 | require_.resolve = function (name) { 164 | return require.resolve(name, dirname); 165 | }; 166 | require_.modules = require.modules; 167 | require_.define = require.define; 168 | require_.cache = require.cache; 169 | var module_ = { 170 | id : filename, 171 | filename: filename, 172 | exports : {}, 173 | loaded : false, 174 | parent: null 175 | }; 176 | 177 | require.modules[filename] = function () { 178 | require.cache[filename] = module_; 179 | fn.call( 180 | module_.exports, 181 | require_, 182 | module_, 183 | module_.exports, 184 | dirname, 185 | filename, 186 | process 187 | ); 188 | module_.loaded = true; 189 | return module_.exports; 190 | }; 191 | }; 192 | })(); 193 | 194 | 195 | require.define("path",function(require,module,exports,__dirname,__filename,process){function filter (xs, fn) { 196 | var res = []; 197 | for (var i = 0; i < xs.length; i++) { 198 | if (fn(xs[i], i, xs)) res.push(xs[i]); 199 | } 200 | return res; 201 | } 202 | 203 | // resolves . and .. elements in a path array with directory names there 204 | // must be no slashes, empty elements, or device names (c:\) in the array 205 | // (so also no leading and trailing slashes - it does not distinguish 206 | // relative and absolute paths) 207 | function normalizeArray(parts, allowAboveRoot) { 208 | // if the path tries to go above the root, `up` ends up > 0 209 | var up = 0; 210 | for (var i = parts.length; i >= 0; i--) { 211 | var last = parts[i]; 212 | if (last == '.') { 213 | parts.splice(i, 1); 214 | } else if (last === '..') { 215 | parts.splice(i, 1); 216 | up++; 217 | } else if (up) { 218 | parts.splice(i, 1); 219 | up--; 220 | } 221 | } 222 | 223 | // if the path is allowed to go above the root, restore leading ..s 224 | if (allowAboveRoot) { 225 | for (; up--; up) { 226 | parts.unshift('..'); 227 | } 228 | } 229 | 230 | return parts; 231 | } 232 | 233 | // Regex to split a filename into [*, dir, basename, ext] 234 | // posix version 235 | var splitPathRe = /^(.+\/(?!$)|\/)?((?:.+?)?(\.[^.]*)?)$/; 236 | 237 | // path.resolve([from ...], to) 238 | // posix version 239 | exports.resolve = function() { 240 | var resolvedPath = '', 241 | resolvedAbsolute = false; 242 | 243 | for (var i = arguments.length; i >= -1 && !resolvedAbsolute; i--) { 244 | var path = (i >= 0) 245 | ? arguments[i] 246 | : process.cwd(); 247 | 248 | // Skip empty and invalid entries 249 | if (typeof path !== 'string' || !path) { 250 | continue; 251 | } 252 | 253 | resolvedPath = path + '/' + resolvedPath; 254 | resolvedAbsolute = path.charAt(0) === '/'; 255 | } 256 | 257 | // At this point the path should be resolved to a full absolute path, but 258 | // handle relative paths to be safe (might happen when process.cwd() fails) 259 | 260 | // Normalize the path 261 | resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) { 262 | return !!p; 263 | }), !resolvedAbsolute).join('/'); 264 | 265 | return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; 266 | }; 267 | 268 | // path.normalize(path) 269 | // posix version 270 | exports.normalize = function(path) { 271 | var isAbsolute = path.charAt(0) === '/', 272 | trailingSlash = path.slice(-1) === '/'; 273 | 274 | // Normalize the path 275 | path = normalizeArray(filter(path.split('/'), function(p) { 276 | return !!p; 277 | }), !isAbsolute).join('/'); 278 | 279 | if (!path && !isAbsolute) { 280 | path = '.'; 281 | } 282 | if (path && trailingSlash) { 283 | path += '/'; 284 | } 285 | 286 | return (isAbsolute ? '/' : '') + path; 287 | }; 288 | 289 | 290 | // posix version 291 | exports.join = function() { 292 | var paths = Array.prototype.slice.call(arguments, 0); 293 | return exports.normalize(filter(paths, function(p, index) { 294 | return p && typeof p === 'string'; 295 | }).join('/')); 296 | }; 297 | 298 | 299 | exports.dirname = function(path) { 300 | var dir = splitPathRe.exec(path)[1] || ''; 301 | var isWindows = false; 302 | if (!dir) { 303 | // No dirname 304 | return '.'; 305 | } else if (dir.length === 1 || 306 | (isWindows && dir.length <= 3 && dir.charAt(1) === ':')) { 307 | // It is just a slash or a drive letter with a slash 308 | return dir; 309 | } else { 310 | // It is a full dirname, strip trailing slash 311 | return dir.substring(0, dir.length - 1); 312 | } 313 | }; 314 | 315 | 316 | exports.basename = function(path, ext) { 317 | var f = splitPathRe.exec(path)[2] || ''; 318 | // TODO: make this comparison case-insensitive on windows? 319 | if (ext && f.substr(-1 * ext.length) === ext) { 320 | f = f.substr(0, f.length - ext.length); 321 | } 322 | return f; 323 | }; 324 | 325 | 326 | exports.extname = function(path) { 327 | return splitPathRe.exec(path)[3] || ''; 328 | }; 329 | }); 330 | 331 | require.define("__browserify_process",function(require,module,exports,__dirname,__filename,process){var process = module.exports = {}; 332 | 333 | process.nextTick = (function () { 334 | var queue = []; 335 | var canPost = typeof window !== 'undefined' 336 | && window.postMessage && window.addEventListener 337 | ; 338 | 339 | if (canPost) { 340 | window.addEventListener('message', function (ev) { 341 | if (ev.source === window && ev.data === 'browserify-tick') { 342 | ev.stopPropagation(); 343 | if (queue.length > 0) { 344 | var fn = queue.shift(); 345 | fn(); 346 | } 347 | } 348 | }, true); 349 | } 350 | 351 | return function (fn) { 352 | if (canPost) { 353 | queue.push(fn); 354 | window.postMessage('browserify-tick', '*'); 355 | } 356 | else setTimeout(fn, 0); 357 | }; 358 | })(); 359 | 360 | process.title = 'browser'; 361 | process.browser = true; 362 | process.env = {}; 363 | process.argv = []; 364 | 365 | process.binding = function (name) { 366 | if (name === 'evals') return (require)('vm') 367 | else throw new Error('No such module. (Possibly not yet loaded)') 368 | }; 369 | 370 | (function () { 371 | var cwd = '/'; 372 | var path; 373 | process.cwd = function () { return cwd }; 374 | process.chdir = function (dir) { 375 | if (!path) path = require('path'); 376 | cwd = path.resolve(dir, cwd); 377 | }; 378 | })(); 379 | }); 380 | 381 | require.define("vm",function(require,module,exports,__dirname,__filename,process){module.exports = require("vm-browserify")}); 382 | 383 | require.define("/node_modules/vm-browserify/package.json",function(require,module,exports,__dirname,__filename,process){module.exports = {"main":"index.js"}}); 384 | 385 | require.define("/node_modules/vm-browserify/index.js",function(require,module,exports,__dirname,__filename,process){var Object_keys = function (obj) { 386 | if (Object.keys) return Object.keys(obj) 387 | else { 388 | var res = []; 389 | for (var key in obj) res.push(key) 390 | return res; 391 | } 392 | }; 393 | 394 | var forEach = function (xs, fn) { 395 | if (xs.forEach) return xs.forEach(fn) 396 | else for (var i = 0; i < xs.length; i++) { 397 | fn(xs[i], i, xs); 398 | } 399 | }; 400 | 401 | var Script = exports.Script = function NodeScript (code) { 402 | if (!(this instanceof Script)) return new Script(code); 403 | this.code = code; 404 | }; 405 | 406 | Script.prototype.runInNewContext = function (context) { 407 | if (!context) context = {}; 408 | 409 | var iframe = document.createElement('iframe'); 410 | if (!iframe.style) iframe.style = {}; 411 | iframe.style.display = 'none'; 412 | 413 | document.body.appendChild(iframe); 414 | 415 | var win = iframe.contentWindow; 416 | 417 | forEach(Object_keys(context), function (key) { 418 | win[key] = context[key]; 419 | }); 420 | 421 | if (!win.eval && win.execScript) { 422 | // win.eval() magically appears when this is called in IE: 423 | win.execScript('null'); 424 | } 425 | 426 | var res = win.eval(this.code); 427 | 428 | forEach(Object_keys(win), function (key) { 429 | context[key] = win[key]; 430 | }); 431 | 432 | document.body.removeChild(iframe); 433 | 434 | return res; 435 | }; 436 | 437 | Script.prototype.runInThisContext = function () { 438 | return eval(this.code); // maybe... 439 | }; 440 | 441 | Script.prototype.runInContext = function (context) { 442 | // seems to be just runInNewContext on magical context objects which are 443 | // otherwise indistinguishable from objects except plain old objects 444 | // for the parameter segfaults node 445 | return this.runInNewContext(context); 446 | }; 447 | 448 | forEach(Object_keys(Script.prototype), function (name) { 449 | exports[name] = Script[name] = function (code) { 450 | var s = Script(code); 451 | return s[name].apply(s, [].slice.call(arguments, 1)); 452 | }; 453 | }); 454 | 455 | exports.createScript = function (code) { 456 | return exports.Script(code); 457 | }; 458 | 459 | exports.createContext = Script.createContext = function (context) { 460 | // not really sure what this one does 461 | // seems to just make a shallow copy 462 | var copy = {}; 463 | if(typeof context === 'object') { 464 | forEach(Object_keys(context), function (key) { 465 | copy[key] = context[key]; 466 | }); 467 | } 468 | return copy; 469 | }; 470 | }); 471 | 472 | require.define("assert",function(require,module,exports,__dirname,__filename,process){// UTILITY 473 | var util = require('util'); 474 | var Buffer = require("buffer").Buffer; 475 | var pSlice = Array.prototype.slice; 476 | 477 | // 1. The assert module provides functions that throw 478 | // AssertionError's when particular conditions are not met. The 479 | // assert module must conform to the following interface. 480 | 481 | var assert = module.exports = ok; 482 | 483 | // 2. The AssertionError is defined in assert. 484 | // new assert.AssertionError({ message: message, 485 | // actual: actual, 486 | // expected: expected }) 487 | 488 | assert.AssertionError = function AssertionError(options) { 489 | this.name = 'AssertionError'; 490 | this.message = options.message; 491 | this.actual = options.actual; 492 | this.expected = options.expected; 493 | this.operator = options.operator; 494 | var stackStartFunction = options.stackStartFunction || fail; 495 | 496 | if (Error.captureStackTrace) { 497 | Error.captureStackTrace(this, stackStartFunction); 498 | } 499 | }; 500 | util.inherits(assert.AssertionError, Error); 501 | 502 | function replacer(key, value) { 503 | if (value === undefined) { 504 | return '' + value; 505 | } 506 | if (typeof value === 'number' && (isNaN(value) || !isFinite(value))) { 507 | return value.toString(); 508 | } 509 | if (typeof value === 'function' || value instanceof RegExp) { 510 | return value.toString(); 511 | } 512 | return value; 513 | } 514 | 515 | function truncate(s, n) { 516 | if (typeof s == 'string') { 517 | return s.length < n ? s : s.slice(0, n); 518 | } else { 519 | return s; 520 | } 521 | } 522 | 523 | assert.AssertionError.prototype.toString = function() { 524 | if (this.message) { 525 | return [this.name + ':', this.message].join(' '); 526 | } else { 527 | return [ 528 | this.name + ':', 529 | truncate(JSON.stringify(this.actual, replacer), 128), 530 | this.operator, 531 | truncate(JSON.stringify(this.expected, replacer), 128) 532 | ].join(' '); 533 | } 534 | }; 535 | 536 | // assert.AssertionError instanceof Error 537 | 538 | assert.AssertionError.__proto__ = Error.prototype; 539 | 540 | // At present only the three keys mentioned above are used and 541 | // understood by the spec. Implementations or sub modules can pass 542 | // other keys to the AssertionError's constructor - they will be 543 | // ignored. 544 | 545 | // 3. All of the following functions must throw an AssertionError 546 | // when a corresponding condition is not met, with a message that 547 | // may be undefined if not provided. All assertion methods provide 548 | // both the actual and expected values to the assertion error for 549 | // display purposes. 550 | 551 | function fail(actual, expected, message, operator, stackStartFunction) { 552 | throw new assert.AssertionError({ 553 | message: message, 554 | actual: actual, 555 | expected: expected, 556 | operator: operator, 557 | stackStartFunction: stackStartFunction 558 | }); 559 | } 560 | 561 | // EXTENSION! allows for well behaved errors defined elsewhere. 562 | assert.fail = fail; 563 | 564 | // 4. Pure assertion tests whether a value is truthy, as determined 565 | // by !!guard. 566 | // assert.ok(guard, message_opt); 567 | // This statement is equivalent to assert.equal(true, guard, 568 | // message_opt);. To test strictly for the value true, use 569 | // assert.strictEqual(true, guard, message_opt);. 570 | 571 | function ok(value, message) { 572 | if (!!!value) fail(value, true, message, '==', assert.ok); 573 | } 574 | assert.ok = ok; 575 | 576 | // 5. The equality assertion tests shallow, coercive equality with 577 | // ==. 578 | // assert.equal(actual, expected, message_opt); 579 | 580 | assert.equal = function equal(actual, expected, message) { 581 | if (actual != expected) fail(actual, expected, message, '==', assert.equal); 582 | }; 583 | 584 | // 6. The non-equality assertion tests for whether two objects are not equal 585 | // with != assert.notEqual(actual, expected, message_opt); 586 | 587 | assert.notEqual = function notEqual(actual, expected, message) { 588 | if (actual == expected) { 589 | fail(actual, expected, message, '!=', assert.notEqual); 590 | } 591 | }; 592 | 593 | // 7. The equivalence assertion tests a deep equality relation. 594 | // assert.deepEqual(actual, expected, message_opt); 595 | 596 | assert.deepEqual = function deepEqual(actual, expected, message) { 597 | if (!_deepEqual(actual, expected)) { 598 | fail(actual, expected, message, 'deepEqual', assert.deepEqual); 599 | } 600 | }; 601 | 602 | function _deepEqual(actual, expected) { 603 | // 7.1. All identical values are equivalent, as determined by ===. 604 | if (actual === expected) { 605 | return true; 606 | 607 | } else if (Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) { 608 | if (actual.length != expected.length) return false; 609 | 610 | for (var i = 0; i < actual.length; i++) { 611 | if (actual[i] !== expected[i]) return false; 612 | } 613 | 614 | return true; 615 | 616 | // 7.2. If the expected value is a Date object, the actual value is 617 | // equivalent if it is also a Date object that refers to the same time. 618 | } else if (actual instanceof Date && expected instanceof Date) { 619 | return actual.getTime() === expected.getTime(); 620 | 621 | // 7.3. Other pairs that do not both pass typeof value == 'object', 622 | // equivalence is determined by ==. 623 | } else if (typeof actual != 'object' && typeof expected != 'object') { 624 | return actual == expected; 625 | 626 | // 7.4. For all other Object pairs, including Array objects, equivalence is 627 | // determined by having the same number of owned properties (as verified 628 | // with Object.prototype.hasOwnProperty.call), the same set of keys 629 | // (although not necessarily the same order), equivalent values for every 630 | // corresponding key, and an identical 'prototype' property. Note: this 631 | // accounts for both named and indexed properties on Arrays. 632 | } else { 633 | return objEquiv(actual, expected); 634 | } 635 | } 636 | 637 | function isUndefinedOrNull(value) { 638 | return value === null || value === undefined; 639 | } 640 | 641 | function isArguments(object) { 642 | return Object.prototype.toString.call(object) == '[object Arguments]'; 643 | } 644 | 645 | function objEquiv(a, b) { 646 | if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) 647 | return false; 648 | // an identical 'prototype' property. 649 | if (a.prototype !== b.prototype) return false; 650 | //~~~I've managed to break Object.keys through screwy arguments passing. 651 | // Converting to array solves the problem. 652 | if (isArguments(a)) { 653 | if (!isArguments(b)) { 654 | return false; 655 | } 656 | a = pSlice.call(a); 657 | b = pSlice.call(b); 658 | return _deepEqual(a, b); 659 | } 660 | try { 661 | var ka = Object.keys(a), 662 | kb = Object.keys(b), 663 | key, i; 664 | } catch (e) {//happens when one is a string literal and the other isn't 665 | return false; 666 | } 667 | // having the same number of owned properties (keys incorporates 668 | // hasOwnProperty) 669 | if (ka.length != kb.length) 670 | return false; 671 | //the same set of keys (although not necessarily the same order), 672 | ka.sort(); 673 | kb.sort(); 674 | //~~~cheap key test 675 | for (i = ka.length - 1; i >= 0; i--) { 676 | if (ka[i] != kb[i]) 677 | return false; 678 | } 679 | //equivalent values for every corresponding key, and 680 | //~~~possibly expensive deep test 681 | for (i = ka.length - 1; i >= 0; i--) { 682 | key = ka[i]; 683 | if (!_deepEqual(a[key], b[key])) return false; 684 | } 685 | return true; 686 | } 687 | 688 | // 8. The non-equivalence assertion tests for any deep inequality. 689 | // assert.notDeepEqual(actual, expected, message_opt); 690 | 691 | assert.notDeepEqual = function notDeepEqual(actual, expected, message) { 692 | if (_deepEqual(actual, expected)) { 693 | fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); 694 | } 695 | }; 696 | 697 | // 9. The strict equality assertion tests strict equality, as determined by ===. 698 | // assert.strictEqual(actual, expected, message_opt); 699 | 700 | assert.strictEqual = function strictEqual(actual, expected, message) { 701 | if (actual !== expected) { 702 | fail(actual, expected, message, '===', assert.strictEqual); 703 | } 704 | }; 705 | 706 | // 10. The strict non-equality assertion tests for strict inequality, as 707 | // determined by !==. assert.notStrictEqual(actual, expected, message_opt); 708 | 709 | assert.notStrictEqual = function notStrictEqual(actual, expected, message) { 710 | if (actual === expected) { 711 | fail(actual, expected, message, '!==', assert.notStrictEqual); 712 | } 713 | }; 714 | 715 | function expectedException(actual, expected) { 716 | if (!actual || !expected) { 717 | return false; 718 | } 719 | 720 | if (expected instanceof RegExp) { 721 | return expected.test(actual); 722 | } else if (actual instanceof expected) { 723 | return true; 724 | } else if (expected.call({}, actual) === true) { 725 | return true; 726 | } 727 | 728 | return false; 729 | } 730 | 731 | function _throws(shouldThrow, block, expected, message) { 732 | var actual; 733 | 734 | if (typeof expected === 'string') { 735 | message = expected; 736 | expected = null; 737 | } 738 | 739 | try { 740 | block(); 741 | } catch (e) { 742 | actual = e; 743 | } 744 | 745 | message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + 746 | (message ? ' ' + message : '.'); 747 | 748 | if (shouldThrow && !actual) { 749 | fail('Missing expected exception' + message); 750 | } 751 | 752 | if (!shouldThrow && expectedException(actual, expected)) { 753 | fail('Got unwanted exception' + message); 754 | } 755 | 756 | if ((shouldThrow && actual && expected && 757 | !expectedException(actual, expected)) || (!shouldThrow && actual)) { 758 | throw actual; 759 | } 760 | } 761 | 762 | // 11. Expected to throw an error: 763 | // assert.throws(block, Error_opt, message_opt); 764 | 765 | assert.throws = function(block, /*optional*/error, /*optional*/message) { 766 | _throws.apply(this, [true].concat(pSlice.call(arguments))); 767 | }; 768 | 769 | // EXTENSION! This is annoying to write outside this module. 770 | assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) { 771 | _throws.apply(this, [false].concat(pSlice.call(arguments))); 772 | }; 773 | 774 | assert.ifError = function(err) { if (err) {throw err;}}; 775 | }); 776 | 777 | require.define("util",function(require,module,exports,__dirname,__filename,process){var events = require('events'); 778 | 779 | exports.print = function () {}; 780 | exports.puts = function () {}; 781 | exports.debug = function() {}; 782 | 783 | exports.inspect = function(obj, showHidden, depth, colors) { 784 | var seen = []; 785 | 786 | var stylize = function(str, styleType) { 787 | // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics 788 | var styles = 789 | { 'bold' : [1, 22], 790 | 'italic' : [3, 23], 791 | 'underline' : [4, 24], 792 | 'inverse' : [7, 27], 793 | 'white' : [37, 39], 794 | 'grey' : [90, 39], 795 | 'black' : [30, 39], 796 | 'blue' : [34, 39], 797 | 'cyan' : [36, 39], 798 | 'green' : [32, 39], 799 | 'magenta' : [35, 39], 800 | 'red' : [31, 39], 801 | 'yellow' : [33, 39] }; 802 | 803 | var style = 804 | { 'special': 'cyan', 805 | 'number': 'blue', 806 | 'boolean': 'yellow', 807 | 'undefined': 'grey', 808 | 'null': 'bold', 809 | 'string': 'green', 810 | 'date': 'magenta', 811 | // "name": intentionally not styling 812 | 'regexp': 'red' }[styleType]; 813 | 814 | if (style) { 815 | return '\033[' + styles[style][0] + 'm' + str + 816 | '\033[' + styles[style][1] + 'm'; 817 | } else { 818 | return str; 819 | } 820 | }; 821 | if (! colors) { 822 | stylize = function(str, styleType) { return str; }; 823 | } 824 | 825 | function format(value, recurseTimes) { 826 | // Provide a hook for user-specified inspect functions. 827 | // Check that value is an object with an inspect function on it 828 | if (value && typeof value.inspect === 'function' && 829 | // Filter out the util module, it's inspect function is special 830 | value !== exports && 831 | // Also filter out any prototype objects using the circular check. 832 | !(value.constructor && value.constructor.prototype === value)) { 833 | return value.inspect(recurseTimes); 834 | } 835 | 836 | // Primitive types cannot have properties 837 | switch (typeof value) { 838 | case 'undefined': 839 | return stylize('undefined', 'undefined'); 840 | 841 | case 'string': 842 | var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') 843 | .replace(/'/g, "\\'") 844 | .replace(/\\"/g, '"') + '\''; 845 | return stylize(simple, 'string'); 846 | 847 | case 'number': 848 | return stylize('' + value, 'number'); 849 | 850 | case 'boolean': 851 | return stylize('' + value, 'boolean'); 852 | } 853 | // For some reason typeof null is "object", so special case here. 854 | if (value === null) { 855 | return stylize('null', 'null'); 856 | } 857 | 858 | // Look up the keys of the object. 859 | var visible_keys = Object_keys(value); 860 | var keys = showHidden ? Object_getOwnPropertyNames(value) : visible_keys; 861 | 862 | // Functions without properties can be shortcutted. 863 | if (typeof value === 'function' && keys.length === 0) { 864 | if (isRegExp(value)) { 865 | return stylize('' + value, 'regexp'); 866 | } else { 867 | var name = value.name ? ': ' + value.name : ''; 868 | return stylize('[Function' + name + ']', 'special'); 869 | } 870 | } 871 | 872 | // Dates without properties can be shortcutted 873 | if (isDate(value) && keys.length === 0) { 874 | return stylize(value.toUTCString(), 'date'); 875 | } 876 | 877 | var base, type, braces; 878 | // Determine the object type 879 | if (isArray(value)) { 880 | type = 'Array'; 881 | braces = ['[', ']']; 882 | } else { 883 | type = 'Object'; 884 | braces = ['{', '}']; 885 | } 886 | 887 | // Make functions say that they are functions 888 | if (typeof value === 'function') { 889 | var n = value.name ? ': ' + value.name : ''; 890 | base = (isRegExp(value)) ? ' ' + value : ' [Function' + n + ']'; 891 | } else { 892 | base = ''; 893 | } 894 | 895 | // Make dates with properties first say the date 896 | if (isDate(value)) { 897 | base = ' ' + value.toUTCString(); 898 | } 899 | 900 | if (keys.length === 0) { 901 | return braces[0] + base + braces[1]; 902 | } 903 | 904 | if (recurseTimes < 0) { 905 | if (isRegExp(value)) { 906 | return stylize('' + value, 'regexp'); 907 | } else { 908 | return stylize('[Object]', 'special'); 909 | } 910 | } 911 | 912 | seen.push(value); 913 | 914 | var output = keys.map(function(key) { 915 | var name, str; 916 | if (value.__lookupGetter__) { 917 | if (value.__lookupGetter__(key)) { 918 | if (value.__lookupSetter__(key)) { 919 | str = stylize('[Getter/Setter]', 'special'); 920 | } else { 921 | str = stylize('[Getter]', 'special'); 922 | } 923 | } else { 924 | if (value.__lookupSetter__(key)) { 925 | str = stylize('[Setter]', 'special'); 926 | } 927 | } 928 | } 929 | if (visible_keys.indexOf(key) < 0) { 930 | name = '[' + key + ']'; 931 | } 932 | if (!str) { 933 | if (seen.indexOf(value[key]) < 0) { 934 | if (recurseTimes === null) { 935 | str = format(value[key]); 936 | } else { 937 | str = format(value[key], recurseTimes - 1); 938 | } 939 | if (str.indexOf('\n') > -1) { 940 | if (isArray(value)) { 941 | str = str.split('\n').map(function(line) { 942 | return ' ' + line; 943 | }).join('\n').substr(2); 944 | } else { 945 | str = '\n' + str.split('\n').map(function(line) { 946 | return ' ' + line; 947 | }).join('\n'); 948 | } 949 | } 950 | } else { 951 | str = stylize('[Circular]', 'special'); 952 | } 953 | } 954 | if (typeof name === 'undefined') { 955 | if (type === 'Array' && key.match(/^\d+$/)) { 956 | return str; 957 | } 958 | name = JSON.stringify('' + key); 959 | if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { 960 | name = name.substr(1, name.length - 2); 961 | name = stylize(name, 'name'); 962 | } else { 963 | name = name.replace(/'/g, "\\'") 964 | .replace(/\\"/g, '"') 965 | .replace(/(^"|"$)/g, "'"); 966 | name = stylize(name, 'string'); 967 | } 968 | } 969 | 970 | return name + ': ' + str; 971 | }); 972 | 973 | seen.pop(); 974 | 975 | var numLinesEst = 0; 976 | var length = output.reduce(function(prev, cur) { 977 | numLinesEst++; 978 | if (cur.indexOf('\n') >= 0) numLinesEst++; 979 | return prev + cur.length + 1; 980 | }, 0); 981 | 982 | if (length > 50) { 983 | output = braces[0] + 984 | (base === '' ? '' : base + '\n ') + 985 | ' ' + 986 | output.join(',\n ') + 987 | ' ' + 988 | braces[1]; 989 | 990 | } else { 991 | output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; 992 | } 993 | 994 | return output; 995 | } 996 | return format(obj, (typeof depth === 'undefined' ? 2 : depth)); 997 | }; 998 | 999 | 1000 | function isArray(ar) { 1001 | return ar instanceof Array || 1002 | Array.isArray(ar) || 1003 | (ar && ar !== Object.prototype && isArray(ar.__proto__)); 1004 | } 1005 | 1006 | 1007 | function isRegExp(re) { 1008 | return re instanceof RegExp || 1009 | (typeof re === 'object' && Object.prototype.toString.call(re) === '[object RegExp]'); 1010 | } 1011 | 1012 | 1013 | function isDate(d) { 1014 | if (d instanceof Date) return true; 1015 | if (typeof d !== 'object') return false; 1016 | var properties = Date.prototype && Object_getOwnPropertyNames(Date.prototype); 1017 | var proto = d.__proto__ && Object_getOwnPropertyNames(d.__proto__); 1018 | return JSON.stringify(proto) === JSON.stringify(properties); 1019 | } 1020 | 1021 | function pad(n) { 1022 | return n < 10 ? '0' + n.toString(10) : n.toString(10); 1023 | } 1024 | 1025 | var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 1026 | 'Oct', 'Nov', 'Dec']; 1027 | 1028 | // 26 Feb 16:19:34 1029 | function timestamp() { 1030 | var d = new Date(); 1031 | var time = [pad(d.getHours()), 1032 | pad(d.getMinutes()), 1033 | pad(d.getSeconds())].join(':'); 1034 | return [d.getDate(), months[d.getMonth()], time].join(' '); 1035 | } 1036 | 1037 | exports.log = function (msg) {}; 1038 | 1039 | exports.pump = null; 1040 | 1041 | var Object_keys = Object.keys || function (obj) { 1042 | var res = []; 1043 | for (var key in obj) res.push(key); 1044 | return res; 1045 | }; 1046 | 1047 | var Object_getOwnPropertyNames = Object.getOwnPropertyNames || function (obj) { 1048 | var res = []; 1049 | for (var key in obj) { 1050 | if (Object.hasOwnProperty.call(obj, key)) res.push(key); 1051 | } 1052 | return res; 1053 | }; 1054 | 1055 | var Object_create = Object.create || function (prototype, properties) { 1056 | // from es5-shim 1057 | var object; 1058 | if (prototype === null) { 1059 | object = { '__proto__' : null }; 1060 | } 1061 | else { 1062 | if (typeof prototype !== 'object') { 1063 | throw new TypeError( 1064 | 'typeof prototype[' + (typeof prototype) + '] != \'object\'' 1065 | ); 1066 | } 1067 | var Type = function () {}; 1068 | Type.prototype = prototype; 1069 | object = new Type(); 1070 | object.__proto__ = prototype; 1071 | } 1072 | if (typeof properties !== 'undefined' && Object.defineProperties) { 1073 | Object.defineProperties(object, properties); 1074 | } 1075 | return object; 1076 | }; 1077 | 1078 | exports.inherits = function(ctor, superCtor) { 1079 | ctor.super_ = superCtor; 1080 | ctor.prototype = Object_create(superCtor.prototype, { 1081 | constructor: { 1082 | value: ctor, 1083 | enumerable: false, 1084 | writable: true, 1085 | configurable: true 1086 | } 1087 | }); 1088 | }; 1089 | }); 1090 | 1091 | require.define("events",function(require,module,exports,__dirname,__filename,process){if (!process.EventEmitter) process.EventEmitter = function () {}; 1092 | 1093 | var EventEmitter = exports.EventEmitter = process.EventEmitter; 1094 | var isArray = typeof Array.isArray === 'function' 1095 | ? Array.isArray 1096 | : function (xs) { 1097 | return Object.prototype.toString.call(xs) === '[object Array]' 1098 | } 1099 | ; 1100 | 1101 | // By default EventEmitters will print a warning if more than 1102 | // 10 listeners are added to it. This is a useful default which 1103 | // helps finding memory leaks. 1104 | // 1105 | // Obviously not all Emitters should be limited to 10. This function allows 1106 | // that to be increased. Set to zero for unlimited. 1107 | var defaultMaxListeners = 10; 1108 | EventEmitter.prototype.setMaxListeners = function(n) { 1109 | if (!this._events) this._events = {}; 1110 | this._events.maxListeners = n; 1111 | }; 1112 | 1113 | 1114 | EventEmitter.prototype.emit = function(type) { 1115 | // If there is no 'error' event listener then throw. 1116 | if (type === 'error') { 1117 | if (!this._events || !this._events.error || 1118 | (isArray(this._events.error) && !this._events.error.length)) 1119 | { 1120 | if (arguments[1] instanceof Error) { 1121 | throw arguments[1]; // Unhandled 'error' event 1122 | } else { 1123 | throw new Error("Uncaught, unspecified 'error' event."); 1124 | } 1125 | return false; 1126 | } 1127 | } 1128 | 1129 | if (!this._events) return false; 1130 | var handler = this._events[type]; 1131 | if (!handler) return false; 1132 | 1133 | if (typeof handler == 'function') { 1134 | switch (arguments.length) { 1135 | // fast cases 1136 | case 1: 1137 | handler.call(this); 1138 | break; 1139 | case 2: 1140 | handler.call(this, arguments[1]); 1141 | break; 1142 | case 3: 1143 | handler.call(this, arguments[1], arguments[2]); 1144 | break; 1145 | // slower 1146 | default: 1147 | var args = Array.prototype.slice.call(arguments, 1); 1148 | handler.apply(this, args); 1149 | } 1150 | return true; 1151 | 1152 | } else if (isArray(handler)) { 1153 | var args = Array.prototype.slice.call(arguments, 1); 1154 | 1155 | var listeners = handler.slice(); 1156 | for (var i = 0, l = listeners.length; i < l; i++) { 1157 | listeners[i].apply(this, args); 1158 | } 1159 | return true; 1160 | 1161 | } else { 1162 | return false; 1163 | } 1164 | }; 1165 | 1166 | // EventEmitter is defined in src/node_events.cc 1167 | // EventEmitter.prototype.emit() is also defined there. 1168 | EventEmitter.prototype.addListener = function(type, listener) { 1169 | if ('function' !== typeof listener) { 1170 | throw new Error('addListener only takes instances of Function'); 1171 | } 1172 | 1173 | if (!this._events) this._events = {}; 1174 | 1175 | // To avoid recursion in the case that type == "newListeners"! Before 1176 | // adding it to the listeners, first emit "newListeners". 1177 | this.emit('newListener', type, listener); 1178 | 1179 | if (!this._events[type]) { 1180 | // Optimize the case of one listener. Don't need the extra array object. 1181 | this._events[type] = listener; 1182 | } else if (isArray(this._events[type])) { 1183 | 1184 | // Check for listener leak 1185 | if (!this._events[type].warned) { 1186 | var m; 1187 | if (this._events.maxListeners !== undefined) { 1188 | m = this._events.maxListeners; 1189 | } else { 1190 | m = defaultMaxListeners; 1191 | } 1192 | 1193 | if (m && m > 0 && this._events[type].length > m) { 1194 | this._events[type].warned = true; 1195 | console.error('(node) warning: possible EventEmitter memory ' + 1196 | 'leak detected. %d listeners added. ' + 1197 | 'Use emitter.setMaxListeners() to increase limit.', 1198 | this._events[type].length); 1199 | console.trace(); 1200 | } 1201 | } 1202 | 1203 | // If we've already got an array, just append. 1204 | this._events[type].push(listener); 1205 | } else { 1206 | // Adding the second element, need to change to array. 1207 | this._events[type] = [this._events[type], listener]; 1208 | } 1209 | 1210 | return this; 1211 | }; 1212 | 1213 | EventEmitter.prototype.on = EventEmitter.prototype.addListener; 1214 | 1215 | EventEmitter.prototype.once = function(type, listener) { 1216 | var self = this; 1217 | self.on(type, function g() { 1218 | self.removeListener(type, g); 1219 | listener.apply(this, arguments); 1220 | }); 1221 | 1222 | return this; 1223 | }; 1224 | 1225 | EventEmitter.prototype.removeListener = function(type, listener) { 1226 | if ('function' !== typeof listener) { 1227 | throw new Error('removeListener only takes instances of Function'); 1228 | } 1229 | 1230 | // does not use listeners(), so no side effect of creating _events[type] 1231 | if (!this._events || !this._events[type]) return this; 1232 | 1233 | var list = this._events[type]; 1234 | 1235 | if (isArray(list)) { 1236 | var i = list.indexOf(listener); 1237 | if (i < 0) return this; 1238 | list.splice(i, 1); 1239 | if (list.length == 0) 1240 | delete this._events[type]; 1241 | } else if (this._events[type] === listener) { 1242 | delete this._events[type]; 1243 | } 1244 | 1245 | return this; 1246 | }; 1247 | 1248 | EventEmitter.prototype.removeAllListeners = function(type) { 1249 | // does not use listeners(), so no side effect of creating _events[type] 1250 | if (type && this._events && this._events[type]) this._events[type] = null; 1251 | return this; 1252 | }; 1253 | 1254 | EventEmitter.prototype.listeners = function(type) { 1255 | if (!this._events) this._events = {}; 1256 | if (!this._events[type]) this._events[type] = []; 1257 | if (!isArray(this._events[type])) { 1258 | this._events[type] = [this._events[type]]; 1259 | } 1260 | return this._events[type]; 1261 | }; 1262 | }); 1263 | 1264 | require.define("buffer",function(require,module,exports,__dirname,__filename,process){module.exports = require("buffer-browserify")}); 1265 | 1266 | require.define("/node_modules/buffer-browserify/package.json",function(require,module,exports,__dirname,__filename,process){module.exports = {"main":"index.js","browserify":"index.js"}}); 1267 | 1268 | require.define("/node_modules/buffer-browserify/index.js",function(require,module,exports,__dirname,__filename,process){function SlowBuffer (size) { 1269 | this.length = size; 1270 | }; 1271 | 1272 | var assert = require('assert'); 1273 | 1274 | exports.INSPECT_MAX_BYTES = 50; 1275 | 1276 | 1277 | function toHex(n) { 1278 | if (n < 16) return '0' + n.toString(16); 1279 | return n.toString(16); 1280 | } 1281 | 1282 | function utf8ToBytes(str) { 1283 | var byteArray = []; 1284 | for (var i = 0; i < str.length; i++) 1285 | if (str.charCodeAt(i) <= 0x7F) 1286 | byteArray.push(str.charCodeAt(i)); 1287 | else { 1288 | var h = encodeURIComponent(str.charAt(i)).substr(1).split('%'); 1289 | for (var j = 0; j < h.length; j++) 1290 | byteArray.push(parseInt(h[j], 16)); 1291 | } 1292 | 1293 | return byteArray; 1294 | } 1295 | 1296 | function asciiToBytes(str) { 1297 | var byteArray = [] 1298 | for (var i = 0; i < str.length; i++ ) 1299 | // Node's code seems to be doing this and not & 0x7F.. 1300 | byteArray.push( str.charCodeAt(i) & 0xFF ); 1301 | 1302 | return byteArray; 1303 | } 1304 | 1305 | function base64ToBytes(str) { 1306 | return require("base64-js").toByteArray(str); 1307 | } 1308 | 1309 | SlowBuffer.byteLength = function (str, encoding) { 1310 | switch (encoding || "utf8") { 1311 | case 'hex': 1312 | return str.length / 2; 1313 | 1314 | case 'utf8': 1315 | case 'utf-8': 1316 | return utf8ToBytes(str).length; 1317 | 1318 | case 'ascii': 1319 | return str.length; 1320 | 1321 | case 'base64': 1322 | return base64ToBytes(str).length; 1323 | 1324 | default: 1325 | throw new Error('Unknown encoding'); 1326 | } 1327 | }; 1328 | 1329 | function blitBuffer(src, dst, offset, length) { 1330 | var pos, i = 0; 1331 | while (i < length) { 1332 | if ((i+offset >= dst.length) || (i >= src.length)) 1333 | break; 1334 | 1335 | dst[i + offset] = src[i]; 1336 | i++; 1337 | } 1338 | return i; 1339 | } 1340 | 1341 | SlowBuffer.prototype.utf8Write = function (string, offset, length) { 1342 | var bytes, pos; 1343 | return SlowBuffer._charsWritten = blitBuffer(utf8ToBytes(string), this, offset, length); 1344 | }; 1345 | 1346 | SlowBuffer.prototype.asciiWrite = function (string, offset, length) { 1347 | var bytes, pos; 1348 | return SlowBuffer._charsWritten = blitBuffer(asciiToBytes(string), this, offset, length); 1349 | }; 1350 | 1351 | SlowBuffer.prototype.base64Write = function (string, offset, length) { 1352 | var bytes, pos; 1353 | return SlowBuffer._charsWritten = blitBuffer(base64ToBytes(string), this, offset, length); 1354 | }; 1355 | 1356 | SlowBuffer.prototype.base64Slice = function (start, end) { 1357 | var bytes = Array.prototype.slice.apply(this, arguments) 1358 | return require("base64-js").fromByteArray(bytes); 1359 | } 1360 | 1361 | function decodeUtf8Char(str) { 1362 | try { 1363 | return decodeURIComponent(str); 1364 | } catch (err) { 1365 | return String.fromCharCode(0xFFFD); // UTF 8 invalid char 1366 | } 1367 | } 1368 | 1369 | SlowBuffer.prototype.utf8Slice = function () { 1370 | var bytes = Array.prototype.slice.apply(this, arguments); 1371 | var res = ""; 1372 | var tmp = ""; 1373 | var i = 0; 1374 | while (i < bytes.length) { 1375 | if (bytes[i] <= 0x7F) { 1376 | res += decodeUtf8Char(tmp) + String.fromCharCode(bytes[i]); 1377 | tmp = ""; 1378 | } else 1379 | tmp += "%" + bytes[i].toString(16); 1380 | 1381 | i++; 1382 | } 1383 | 1384 | return res + decodeUtf8Char(tmp); 1385 | } 1386 | 1387 | SlowBuffer.prototype.asciiSlice = function () { 1388 | var bytes = Array.prototype.slice.apply(this, arguments); 1389 | var ret = ""; 1390 | for (var i = 0; i < bytes.length; i++) 1391 | ret += String.fromCharCode(bytes[i]); 1392 | return ret; 1393 | } 1394 | 1395 | SlowBuffer.prototype.inspect = function() { 1396 | var out = [], 1397 | len = this.length; 1398 | for (var i = 0; i < len; i++) { 1399 | out[i] = toHex(this[i]); 1400 | if (i == exports.INSPECT_MAX_BYTES) { 1401 | out[i + 1] = '...'; 1402 | break; 1403 | } 1404 | } 1405 | return ''; 1406 | }; 1407 | 1408 | 1409 | SlowBuffer.prototype.hexSlice = function(start, end) { 1410 | var len = this.length; 1411 | 1412 | if (!start || start < 0) start = 0; 1413 | if (!end || end < 0 || end > len) end = len; 1414 | 1415 | var out = ''; 1416 | for (var i = start; i < end; i++) { 1417 | out += toHex(this[i]); 1418 | } 1419 | return out; 1420 | }; 1421 | 1422 | 1423 | SlowBuffer.prototype.toString = function(encoding, start, end) { 1424 | encoding = String(encoding || 'utf8').toLowerCase(); 1425 | start = +start || 0; 1426 | if (typeof end == 'undefined') end = this.length; 1427 | 1428 | // Fastpath empty strings 1429 | if (+end == start) { 1430 | return ''; 1431 | } 1432 | 1433 | switch (encoding) { 1434 | case 'hex': 1435 | return this.hexSlice(start, end); 1436 | 1437 | case 'utf8': 1438 | case 'utf-8': 1439 | return this.utf8Slice(start, end); 1440 | 1441 | case 'ascii': 1442 | return this.asciiSlice(start, end); 1443 | 1444 | case 'binary': 1445 | return this.binarySlice(start, end); 1446 | 1447 | case 'base64': 1448 | return this.base64Slice(start, end); 1449 | 1450 | case 'ucs2': 1451 | case 'ucs-2': 1452 | return this.ucs2Slice(start, end); 1453 | 1454 | default: 1455 | throw new Error('Unknown encoding'); 1456 | } 1457 | }; 1458 | 1459 | 1460 | SlowBuffer.prototype.hexWrite = function(string, offset, length) { 1461 | offset = +offset || 0; 1462 | var remaining = this.length - offset; 1463 | if (!length) { 1464 | length = remaining; 1465 | } else { 1466 | length = +length; 1467 | if (length > remaining) { 1468 | length = remaining; 1469 | } 1470 | } 1471 | 1472 | // must be an even number of digits 1473 | var strLen = string.length; 1474 | if (strLen % 2) { 1475 | throw new Error('Invalid hex string'); 1476 | } 1477 | if (length > strLen / 2) { 1478 | length = strLen / 2; 1479 | } 1480 | for (var i = 0; i < length; i++) { 1481 | var byte = parseInt(string.substr(i * 2, 2), 16); 1482 | if (isNaN(byte)) throw new Error('Invalid hex string'); 1483 | this[offset + i] = byte; 1484 | } 1485 | SlowBuffer._charsWritten = i * 2; 1486 | return i; 1487 | }; 1488 | 1489 | 1490 | SlowBuffer.prototype.write = function(string, offset, length, encoding) { 1491 | // Support both (string, offset, length, encoding) 1492 | // and the legacy (string, encoding, offset, length) 1493 | if (isFinite(offset)) { 1494 | if (!isFinite(length)) { 1495 | encoding = length; 1496 | length = undefined; 1497 | } 1498 | } else { // legacy 1499 | var swap = encoding; 1500 | encoding = offset; 1501 | offset = length; 1502 | length = swap; 1503 | } 1504 | 1505 | offset = +offset || 0; 1506 | var remaining = this.length - offset; 1507 | if (!length) { 1508 | length = remaining; 1509 | } else { 1510 | length = +length; 1511 | if (length > remaining) { 1512 | length = remaining; 1513 | } 1514 | } 1515 | encoding = String(encoding || 'utf8').toLowerCase(); 1516 | 1517 | switch (encoding) { 1518 | case 'hex': 1519 | return this.hexWrite(string, offset, length); 1520 | 1521 | case 'utf8': 1522 | case 'utf-8': 1523 | return this.utf8Write(string, offset, length); 1524 | 1525 | case 'ascii': 1526 | return this.asciiWrite(string, offset, length); 1527 | 1528 | case 'binary': 1529 | return this.binaryWrite(string, offset, length); 1530 | 1531 | case 'base64': 1532 | return this.base64Write(string, offset, length); 1533 | 1534 | case 'ucs2': 1535 | case 'ucs-2': 1536 | return this.ucs2Write(string, offset, length); 1537 | 1538 | default: 1539 | throw new Error('Unknown encoding'); 1540 | } 1541 | }; 1542 | 1543 | 1544 | // slice(start, end) 1545 | SlowBuffer.prototype.slice = function(start, end) { 1546 | if (end === undefined) end = this.length; 1547 | 1548 | if (end > this.length) { 1549 | throw new Error('oob'); 1550 | } 1551 | if (start > end) { 1552 | throw new Error('oob'); 1553 | } 1554 | 1555 | return new Buffer(this, end - start, +start); 1556 | }; 1557 | 1558 | 1559 | function coerce(length) { 1560 | // Coerce length to a number (possibly NaN), round up 1561 | // in case it's fractional (e.g. 123.456) then do a 1562 | // double negate to coerce a NaN to 0. Easy, right? 1563 | length = ~~Math.ceil(+length); 1564 | return length < 0 ? 0 : length; 1565 | } 1566 | 1567 | 1568 | // Buffer 1569 | 1570 | function Buffer(subject, encoding, offset) { 1571 | if (!(this instanceof Buffer)) { 1572 | return new Buffer(subject, encoding, offset); 1573 | } 1574 | 1575 | var type; 1576 | 1577 | // Are we slicing? 1578 | if (typeof offset === 'number') { 1579 | this.length = coerce(encoding); 1580 | this.parent = subject; 1581 | this.offset = offset; 1582 | } else { 1583 | // Find the length 1584 | switch (type = typeof subject) { 1585 | case 'number': 1586 | this.length = coerce(subject); 1587 | break; 1588 | 1589 | case 'string': 1590 | this.length = Buffer.byteLength(subject, encoding); 1591 | break; 1592 | 1593 | case 'object': // Assume object is an array 1594 | this.length = coerce(subject.length); 1595 | break; 1596 | 1597 | default: 1598 | throw new Error('First argument needs to be a number, ' + 1599 | 'array or string.'); 1600 | } 1601 | 1602 | if (this.length > Buffer.poolSize) { 1603 | // Big buffer, just alloc one. 1604 | this.parent = new SlowBuffer(this.length); 1605 | this.offset = 0; 1606 | 1607 | } else { 1608 | // Small buffer. 1609 | if (!pool || pool.length - pool.used < this.length) allocPool(); 1610 | this.parent = pool; 1611 | this.offset = pool.used; 1612 | pool.used += this.length; 1613 | } 1614 | 1615 | // Treat array-ish objects as a byte array. 1616 | if (isArrayIsh(subject)) { 1617 | for (var i = 0; i < this.length; i++) { 1618 | this.parent[i + this.offset] = subject[i]; 1619 | } 1620 | } else if (type == 'string') { 1621 | // We are a string 1622 | this.length = this.write(subject, 0, encoding); 1623 | } 1624 | } 1625 | 1626 | } 1627 | 1628 | function isArrayIsh(subject) { 1629 | return Array.isArray(subject) || Buffer.isBuffer(subject) || 1630 | subject && typeof subject === 'object' && 1631 | typeof subject.length === 'number'; 1632 | } 1633 | 1634 | exports.SlowBuffer = SlowBuffer; 1635 | exports.Buffer = Buffer; 1636 | 1637 | Buffer.poolSize = 8 * 1024; 1638 | var pool; 1639 | 1640 | function allocPool() { 1641 | pool = new SlowBuffer(Buffer.poolSize); 1642 | pool.used = 0; 1643 | } 1644 | 1645 | 1646 | // Static methods 1647 | Buffer.isBuffer = function isBuffer(b) { 1648 | return b instanceof Buffer || b instanceof SlowBuffer; 1649 | }; 1650 | 1651 | 1652 | // Inspect 1653 | Buffer.prototype.inspect = function inspect() { 1654 | var out = [], 1655 | len = this.length; 1656 | 1657 | for (var i = 0; i < len; i++) { 1658 | out[i] = toHex(this.parent[i + this.offset]); 1659 | if (i == exports.INSPECT_MAX_BYTES) { 1660 | out[i + 1] = '...'; 1661 | break; 1662 | } 1663 | } 1664 | 1665 | return ''; 1666 | }; 1667 | 1668 | 1669 | Buffer.prototype.get = function get(i) { 1670 | if (i < 0 || i >= this.length) throw new Error('oob'); 1671 | return this.parent[this.offset + i]; 1672 | }; 1673 | 1674 | 1675 | Buffer.prototype.set = function set(i, v) { 1676 | if (i < 0 || i >= this.length) throw new Error('oob'); 1677 | return this.parent[this.offset + i] = v; 1678 | }; 1679 | 1680 | 1681 | // write(string, offset = 0, length = buffer.length-offset, encoding = 'utf8') 1682 | Buffer.prototype.write = function(string, offset, length, encoding) { 1683 | // Support both (string, offset, length, encoding) 1684 | // and the legacy (string, encoding, offset, length) 1685 | if (isFinite(offset)) { 1686 | if (!isFinite(length)) { 1687 | encoding = length; 1688 | length = undefined; 1689 | } 1690 | } else { // legacy 1691 | var swap = encoding; 1692 | encoding = offset; 1693 | offset = length; 1694 | length = swap; 1695 | } 1696 | 1697 | offset = +offset || 0; 1698 | var remaining = this.length - offset; 1699 | if (!length) { 1700 | length = remaining; 1701 | } else { 1702 | length = +length; 1703 | if (length > remaining) { 1704 | length = remaining; 1705 | } 1706 | } 1707 | encoding = String(encoding || 'utf8').toLowerCase(); 1708 | 1709 | var ret; 1710 | switch (encoding) { 1711 | case 'hex': 1712 | ret = this.parent.hexWrite(string, this.offset + offset, length); 1713 | break; 1714 | 1715 | case 'utf8': 1716 | case 'utf-8': 1717 | ret = this.parent.utf8Write(string, this.offset + offset, length); 1718 | break; 1719 | 1720 | case 'ascii': 1721 | ret = this.parent.asciiWrite(string, this.offset + offset, length); 1722 | break; 1723 | 1724 | case 'binary': 1725 | ret = this.parent.binaryWrite(string, this.offset + offset, length); 1726 | break; 1727 | 1728 | case 'base64': 1729 | // Warning: maxLength not taken into account in base64Write 1730 | ret = this.parent.base64Write(string, this.offset + offset, length); 1731 | break; 1732 | 1733 | case 'ucs2': 1734 | case 'ucs-2': 1735 | ret = this.parent.ucs2Write(string, this.offset + offset, length); 1736 | break; 1737 | 1738 | default: 1739 | throw new Error('Unknown encoding'); 1740 | } 1741 | 1742 | Buffer._charsWritten = SlowBuffer._charsWritten; 1743 | 1744 | return ret; 1745 | }; 1746 | 1747 | 1748 | // toString(encoding, start=0, end=buffer.length) 1749 | Buffer.prototype.toString = function(encoding, start, end) { 1750 | encoding = String(encoding || 'utf8').toLowerCase(); 1751 | 1752 | if (typeof start == 'undefined' || start < 0) { 1753 | start = 0; 1754 | } else if (start > this.length) { 1755 | start = this.length; 1756 | } 1757 | 1758 | if (typeof end == 'undefined' || end > this.length) { 1759 | end = this.length; 1760 | } else if (end < 0) { 1761 | end = 0; 1762 | } 1763 | 1764 | start = start + this.offset; 1765 | end = end + this.offset; 1766 | 1767 | switch (encoding) { 1768 | case 'hex': 1769 | return this.parent.hexSlice(start, end); 1770 | 1771 | case 'utf8': 1772 | case 'utf-8': 1773 | return this.parent.utf8Slice(start, end); 1774 | 1775 | case 'ascii': 1776 | return this.parent.asciiSlice(start, end); 1777 | 1778 | case 'binary': 1779 | return this.parent.binarySlice(start, end); 1780 | 1781 | case 'base64': 1782 | return this.parent.base64Slice(start, end); 1783 | 1784 | case 'ucs2': 1785 | case 'ucs-2': 1786 | return this.parent.ucs2Slice(start, end); 1787 | 1788 | default: 1789 | throw new Error('Unknown encoding'); 1790 | } 1791 | }; 1792 | 1793 | 1794 | // byteLength 1795 | Buffer.byteLength = SlowBuffer.byteLength; 1796 | 1797 | 1798 | // fill(value, start=0, end=buffer.length) 1799 | Buffer.prototype.fill = function fill(value, start, end) { 1800 | value || (value = 0); 1801 | start || (start = 0); 1802 | end || (end = this.length); 1803 | 1804 | if (typeof value === 'string') { 1805 | value = value.charCodeAt(0); 1806 | } 1807 | if (!(typeof value === 'number') || isNaN(value)) { 1808 | throw new Error('value is not a number'); 1809 | } 1810 | 1811 | if (end < start) throw new Error('end < start'); 1812 | 1813 | // Fill 0 bytes; we're done 1814 | if (end === start) return 0; 1815 | if (this.length == 0) return 0; 1816 | 1817 | if (start < 0 || start >= this.length) { 1818 | throw new Error('start out of bounds'); 1819 | } 1820 | 1821 | if (end < 0 || end > this.length) { 1822 | throw new Error('end out of bounds'); 1823 | } 1824 | 1825 | return this.parent.fill(value, 1826 | start + this.offset, 1827 | end + this.offset); 1828 | }; 1829 | 1830 | 1831 | // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) 1832 | Buffer.prototype.copy = function(target, target_start, start, end) { 1833 | var source = this; 1834 | start || (start = 0); 1835 | end || (end = this.length); 1836 | target_start || (target_start = 0); 1837 | 1838 | if (end < start) throw new Error('sourceEnd < sourceStart'); 1839 | 1840 | // Copy 0 bytes; we're done 1841 | if (end === start) return 0; 1842 | if (target.length == 0 || source.length == 0) return 0; 1843 | 1844 | if (target_start < 0 || target_start >= target.length) { 1845 | throw new Error('targetStart out of bounds'); 1846 | } 1847 | 1848 | if (start < 0 || start >= source.length) { 1849 | throw new Error('sourceStart out of bounds'); 1850 | } 1851 | 1852 | if (end < 0 || end > source.length) { 1853 | throw new Error('sourceEnd out of bounds'); 1854 | } 1855 | 1856 | // Are we oob? 1857 | if (end > this.length) { 1858 | end = this.length; 1859 | } 1860 | 1861 | if (target.length - target_start < end - start) { 1862 | end = target.length - target_start + start; 1863 | } 1864 | 1865 | return this.parent.copy(target.parent, 1866 | target_start + target.offset, 1867 | start + this.offset, 1868 | end + this.offset); 1869 | }; 1870 | 1871 | 1872 | // slice(start, end) 1873 | Buffer.prototype.slice = function(start, end) { 1874 | if (end === undefined) end = this.length; 1875 | if (end > this.length) throw new Error('oob'); 1876 | if (start > end) throw new Error('oob'); 1877 | 1878 | return new Buffer(this.parent, end - start, +start + this.offset); 1879 | }; 1880 | 1881 | 1882 | // Legacy methods for backwards compatibility. 1883 | 1884 | Buffer.prototype.utf8Slice = function(start, end) { 1885 | return this.toString('utf8', start, end); 1886 | }; 1887 | 1888 | Buffer.prototype.binarySlice = function(start, end) { 1889 | return this.toString('binary', start, end); 1890 | }; 1891 | 1892 | Buffer.prototype.asciiSlice = function(start, end) { 1893 | return this.toString('ascii', start, end); 1894 | }; 1895 | 1896 | Buffer.prototype.utf8Write = function(string, offset) { 1897 | return this.write(string, offset, 'utf8'); 1898 | }; 1899 | 1900 | Buffer.prototype.binaryWrite = function(string, offset) { 1901 | return this.write(string, offset, 'binary'); 1902 | }; 1903 | 1904 | Buffer.prototype.asciiWrite = function(string, offset) { 1905 | return this.write(string, offset, 'ascii'); 1906 | }; 1907 | 1908 | Buffer.prototype.readUInt8 = function(offset, noAssert) { 1909 | var buffer = this; 1910 | 1911 | if (!noAssert) { 1912 | assert.ok(offset !== undefined && offset !== null, 1913 | 'missing offset'); 1914 | 1915 | assert.ok(offset < buffer.length, 1916 | 'Trying to read beyond buffer length'); 1917 | } 1918 | 1919 | return buffer[offset]; 1920 | }; 1921 | 1922 | function readUInt16(buffer, offset, isBigEndian, noAssert) { 1923 | var val = 0; 1924 | 1925 | 1926 | if (!noAssert) { 1927 | assert.ok(typeof (isBigEndian) === 'boolean', 1928 | 'missing or invalid endian'); 1929 | 1930 | assert.ok(offset !== undefined && offset !== null, 1931 | 'missing offset'); 1932 | 1933 | assert.ok(offset + 1 < buffer.length, 1934 | 'Trying to read beyond buffer length'); 1935 | } 1936 | 1937 | if (isBigEndian) { 1938 | val = buffer[offset] << 8; 1939 | val |= buffer[offset + 1]; 1940 | } else { 1941 | val = buffer[offset]; 1942 | val |= buffer[offset + 1] << 8; 1943 | } 1944 | 1945 | return val; 1946 | } 1947 | 1948 | Buffer.prototype.readUInt16LE = function(offset, noAssert) { 1949 | return readUInt16(this, offset, false, noAssert); 1950 | }; 1951 | 1952 | Buffer.prototype.readUInt16BE = function(offset, noAssert) { 1953 | return readUInt16(this, offset, true, noAssert); 1954 | }; 1955 | 1956 | function readUInt32(buffer, offset, isBigEndian, noAssert) { 1957 | var val = 0; 1958 | 1959 | if (!noAssert) { 1960 | assert.ok(typeof (isBigEndian) === 'boolean', 1961 | 'missing or invalid endian'); 1962 | 1963 | assert.ok(offset !== undefined && offset !== null, 1964 | 'missing offset'); 1965 | 1966 | assert.ok(offset + 3 < buffer.length, 1967 | 'Trying to read beyond buffer length'); 1968 | } 1969 | 1970 | if (isBigEndian) { 1971 | val = buffer[offset + 1] << 16; 1972 | val |= buffer[offset + 2] << 8; 1973 | val |= buffer[offset + 3]; 1974 | val = val + (buffer[offset] << 24 >>> 0); 1975 | } else { 1976 | val = buffer[offset + 2] << 16; 1977 | val |= buffer[offset + 1] << 8; 1978 | val |= buffer[offset]; 1979 | val = val + (buffer[offset + 3] << 24 >>> 0); 1980 | } 1981 | 1982 | return val; 1983 | } 1984 | 1985 | Buffer.prototype.readUInt32LE = function(offset, noAssert) { 1986 | return readUInt32(this, offset, false, noAssert); 1987 | }; 1988 | 1989 | Buffer.prototype.readUInt32BE = function(offset, noAssert) { 1990 | return readUInt32(this, offset, true, noAssert); 1991 | }; 1992 | 1993 | 1994 | /* 1995 | * Signed integer types, yay team! A reminder on how two's complement actually 1996 | * works. The first bit is the signed bit, i.e. tells us whether or not the 1997 | * number should be positive or negative. If the two's complement value is 1998 | * positive, then we're done, as it's equivalent to the unsigned representation. 1999 | * 2000 | * Now if the number is positive, you're pretty much done, you can just leverage 2001 | * the unsigned translations and return those. Unfortunately, negative numbers 2002 | * aren't quite that straightforward. 2003 | * 2004 | * At first glance, one might be inclined to use the traditional formula to 2005 | * translate binary numbers between the positive and negative values in two's 2006 | * complement. (Though it doesn't quite work for the most negative value) 2007 | * Mainly: 2008 | * - invert all the bits 2009 | * - add one to the result 2010 | * 2011 | * Of course, this doesn't quite work in Javascript. Take for example the value 2012 | * of -128. This could be represented in 16 bits (big-endian) as 0xff80. But of 2013 | * course, Javascript will do the following: 2014 | * 2015 | * > ~0xff80 2016 | * -65409 2017 | * 2018 | * Whoh there, Javascript, that's not quite right. But wait, according to 2019 | * Javascript that's perfectly correct. When Javascript ends up seeing the 2020 | * constant 0xff80, it has no notion that it is actually a signed number. It 2021 | * assumes that we've input the unsigned value 0xff80. Thus, when it does the 2022 | * binary negation, it casts it into a signed value, (positive 0xff80). Then 2023 | * when you perform binary negation on that, it turns it into a negative number. 2024 | * 2025 | * Instead, we're going to have to use the following general formula, that works 2026 | * in a rather Javascript friendly way. I'm glad we don't support this kind of 2027 | * weird numbering scheme in the kernel. 2028 | * 2029 | * (BIT-MAX - (unsigned)val + 1) * -1 2030 | * 2031 | * The astute observer, may think that this doesn't make sense for 8-bit numbers 2032 | * (really it isn't necessary for them). However, when you get 16-bit numbers, 2033 | * you do. Let's go back to our prior example and see how this will look: 2034 | * 2035 | * (0xffff - 0xff80 + 1) * -1 2036 | * (0x007f + 1) * -1 2037 | * (0x0080) * -1 2038 | */ 2039 | Buffer.prototype.readInt8 = function(offset, noAssert) { 2040 | var buffer = this; 2041 | var neg; 2042 | 2043 | if (!noAssert) { 2044 | assert.ok(offset !== undefined && offset !== null, 2045 | 'missing offset'); 2046 | 2047 | assert.ok(offset < buffer.length, 2048 | 'Trying to read beyond buffer length'); 2049 | } 2050 | 2051 | neg = buffer[offset] & 0x80; 2052 | if (!neg) { 2053 | return (buffer[offset]); 2054 | } 2055 | 2056 | return ((0xff - buffer[offset] + 1) * -1); 2057 | }; 2058 | 2059 | function readInt16(buffer, offset, isBigEndian, noAssert) { 2060 | var neg, val; 2061 | 2062 | if (!noAssert) { 2063 | assert.ok(typeof (isBigEndian) === 'boolean', 2064 | 'missing or invalid endian'); 2065 | 2066 | assert.ok(offset !== undefined && offset !== null, 2067 | 'missing offset'); 2068 | 2069 | assert.ok(offset + 1 < buffer.length, 2070 | 'Trying to read beyond buffer length'); 2071 | } 2072 | 2073 | val = readUInt16(buffer, offset, isBigEndian, noAssert); 2074 | neg = val & 0x8000; 2075 | if (!neg) { 2076 | return val; 2077 | } 2078 | 2079 | return (0xffff - val + 1) * -1; 2080 | } 2081 | 2082 | Buffer.prototype.readInt16LE = function(offset, noAssert) { 2083 | return readInt16(this, offset, false, noAssert); 2084 | }; 2085 | 2086 | Buffer.prototype.readInt16BE = function(offset, noAssert) { 2087 | return readInt16(this, offset, true, noAssert); 2088 | }; 2089 | 2090 | function readInt32(buffer, offset, isBigEndian, noAssert) { 2091 | var neg, val; 2092 | 2093 | if (!noAssert) { 2094 | assert.ok(typeof (isBigEndian) === 'boolean', 2095 | 'missing or invalid endian'); 2096 | 2097 | assert.ok(offset !== undefined && offset !== null, 2098 | 'missing offset'); 2099 | 2100 | assert.ok(offset + 3 < buffer.length, 2101 | 'Trying to read beyond buffer length'); 2102 | } 2103 | 2104 | val = readUInt32(buffer, offset, isBigEndian, noAssert); 2105 | neg = val & 0x80000000; 2106 | if (!neg) { 2107 | return (val); 2108 | } 2109 | 2110 | return (0xffffffff - val + 1) * -1; 2111 | } 2112 | 2113 | Buffer.prototype.readInt32LE = function(offset, noAssert) { 2114 | return readInt32(this, offset, false, noAssert); 2115 | }; 2116 | 2117 | Buffer.prototype.readInt32BE = function(offset, noAssert) { 2118 | return readInt32(this, offset, true, noAssert); 2119 | }; 2120 | 2121 | function readFloat(buffer, offset, isBigEndian, noAssert) { 2122 | if (!noAssert) { 2123 | assert.ok(typeof (isBigEndian) === 'boolean', 2124 | 'missing or invalid endian'); 2125 | 2126 | assert.ok(offset + 3 < buffer.length, 2127 | 'Trying to read beyond buffer length'); 2128 | } 2129 | 2130 | return require('./buffer_ieee754').readIEEE754(buffer, offset, isBigEndian, 2131 | 23, 4); 2132 | } 2133 | 2134 | Buffer.prototype.readFloatLE = function(offset, noAssert) { 2135 | return readFloat(this, offset, false, noAssert); 2136 | }; 2137 | 2138 | Buffer.prototype.readFloatBE = function(offset, noAssert) { 2139 | return readFloat(this, offset, true, noAssert); 2140 | }; 2141 | 2142 | function readDouble(buffer, offset, isBigEndian, noAssert) { 2143 | if (!noAssert) { 2144 | assert.ok(typeof (isBigEndian) === 'boolean', 2145 | 'missing or invalid endian'); 2146 | 2147 | assert.ok(offset + 7 < buffer.length, 2148 | 'Trying to read beyond buffer length'); 2149 | } 2150 | 2151 | return require('./buffer_ieee754').readIEEE754(buffer, offset, isBigEndian, 2152 | 52, 8); 2153 | } 2154 | 2155 | Buffer.prototype.readDoubleLE = function(offset, noAssert) { 2156 | return readDouble(this, offset, false, noAssert); 2157 | }; 2158 | 2159 | Buffer.prototype.readDoubleBE = function(offset, noAssert) { 2160 | return readDouble(this, offset, true, noAssert); 2161 | }; 2162 | 2163 | 2164 | /* 2165 | * We have to make sure that the value is a valid integer. This means that it is 2166 | * non-negative. It has no fractional component and that it does not exceed the 2167 | * maximum allowed value. 2168 | * 2169 | * value The number to check for validity 2170 | * 2171 | * max The maximum value 2172 | */ 2173 | function verifuint(value, max) { 2174 | assert.ok(typeof (value) == 'number', 2175 | 'cannot write a non-number as a number'); 2176 | 2177 | assert.ok(value >= 0, 2178 | 'specified a negative value for writing an unsigned value'); 2179 | 2180 | assert.ok(value <= max, 'value is larger than maximum value for type'); 2181 | 2182 | assert.ok(Math.floor(value) === value, 'value has a fractional component'); 2183 | } 2184 | 2185 | Buffer.prototype.writeUInt8 = function(value, offset, noAssert) { 2186 | var buffer = this; 2187 | 2188 | if (!noAssert) { 2189 | assert.ok(value !== undefined && value !== null, 2190 | 'missing value'); 2191 | 2192 | assert.ok(offset !== undefined && offset !== null, 2193 | 'missing offset'); 2194 | 2195 | assert.ok(offset < buffer.length, 2196 | 'trying to write beyond buffer length'); 2197 | 2198 | verifuint(value, 0xff); 2199 | } 2200 | 2201 | buffer[offset] = value; 2202 | }; 2203 | 2204 | function writeUInt16(buffer, value, offset, isBigEndian, noAssert) { 2205 | if (!noAssert) { 2206 | assert.ok(value !== undefined && value !== null, 2207 | 'missing value'); 2208 | 2209 | assert.ok(typeof (isBigEndian) === 'boolean', 2210 | 'missing or invalid endian'); 2211 | 2212 | assert.ok(offset !== undefined && offset !== null, 2213 | 'missing offset'); 2214 | 2215 | assert.ok(offset + 1 < buffer.length, 2216 | 'trying to write beyond buffer length'); 2217 | 2218 | verifuint(value, 0xffff); 2219 | } 2220 | 2221 | if (isBigEndian) { 2222 | buffer[offset] = (value & 0xff00) >>> 8; 2223 | buffer[offset + 1] = value & 0x00ff; 2224 | } else { 2225 | buffer[offset + 1] = (value & 0xff00) >>> 8; 2226 | buffer[offset] = value & 0x00ff; 2227 | } 2228 | } 2229 | 2230 | Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) { 2231 | writeUInt16(this, value, offset, false, noAssert); 2232 | }; 2233 | 2234 | Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) { 2235 | writeUInt16(this, value, offset, true, noAssert); 2236 | }; 2237 | 2238 | function writeUInt32(buffer, value, offset, isBigEndian, noAssert) { 2239 | if (!noAssert) { 2240 | assert.ok(value !== undefined && value !== null, 2241 | 'missing value'); 2242 | 2243 | assert.ok(typeof (isBigEndian) === 'boolean', 2244 | 'missing or invalid endian'); 2245 | 2246 | assert.ok(offset !== undefined && offset !== null, 2247 | 'missing offset'); 2248 | 2249 | assert.ok(offset + 3 < buffer.length, 2250 | 'trying to write beyond buffer length'); 2251 | 2252 | verifuint(value, 0xffffffff); 2253 | } 2254 | 2255 | if (isBigEndian) { 2256 | buffer[offset] = (value >>> 24) & 0xff; 2257 | buffer[offset + 1] = (value >>> 16) & 0xff; 2258 | buffer[offset + 2] = (value >>> 8) & 0xff; 2259 | buffer[offset + 3] = value & 0xff; 2260 | } else { 2261 | buffer[offset + 3] = (value >>> 24) & 0xff; 2262 | buffer[offset + 2] = (value >>> 16) & 0xff; 2263 | buffer[offset + 1] = (value >>> 8) & 0xff; 2264 | buffer[offset] = value & 0xff; 2265 | } 2266 | } 2267 | 2268 | Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) { 2269 | writeUInt32(this, value, offset, false, noAssert); 2270 | }; 2271 | 2272 | Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) { 2273 | writeUInt32(this, value, offset, true, noAssert); 2274 | }; 2275 | 2276 | 2277 | /* 2278 | * We now move onto our friends in the signed number category. Unlike unsigned 2279 | * numbers, we're going to have to worry a bit more about how we put values into 2280 | * arrays. Since we are only worrying about signed 32-bit values, we're in 2281 | * slightly better shape. Unfortunately, we really can't do our favorite binary 2282 | * & in this system. It really seems to do the wrong thing. For example: 2283 | * 2284 | * > -32 & 0xff 2285 | * 224 2286 | * 2287 | * What's happening above is really: 0xe0 & 0xff = 0xe0. However, the results of 2288 | * this aren't treated as a signed number. Ultimately a bad thing. 2289 | * 2290 | * What we're going to want to do is basically create the unsigned equivalent of 2291 | * our representation and pass that off to the wuint* functions. To do that 2292 | * we're going to do the following: 2293 | * 2294 | * - if the value is positive 2295 | * we can pass it directly off to the equivalent wuint 2296 | * - if the value is negative 2297 | * we do the following computation: 2298 | * mb + val + 1, where 2299 | * mb is the maximum unsigned value in that byte size 2300 | * val is the Javascript negative integer 2301 | * 2302 | * 2303 | * As a concrete value, take -128. In signed 16 bits this would be 0xff80. If 2304 | * you do out the computations: 2305 | * 2306 | * 0xffff - 128 + 1 2307 | * 0xffff - 127 2308 | * 0xff80 2309 | * 2310 | * You can then encode this value as the signed version. This is really rather 2311 | * hacky, but it should work and get the job done which is our goal here. 2312 | */ 2313 | 2314 | /* 2315 | * A series of checks to make sure we actually have a signed 32-bit number 2316 | */ 2317 | function verifsint(value, max, min) { 2318 | assert.ok(typeof (value) == 'number', 2319 | 'cannot write a non-number as a number'); 2320 | 2321 | assert.ok(value <= max, 'value larger than maximum allowed value'); 2322 | 2323 | assert.ok(value >= min, 'value smaller than minimum allowed value'); 2324 | 2325 | assert.ok(Math.floor(value) === value, 'value has a fractional component'); 2326 | } 2327 | 2328 | function verifIEEE754(value, max, min) { 2329 | assert.ok(typeof (value) == 'number', 2330 | 'cannot write a non-number as a number'); 2331 | 2332 | assert.ok(value <= max, 'value larger than maximum allowed value'); 2333 | 2334 | assert.ok(value >= min, 'value smaller than minimum allowed value'); 2335 | } 2336 | 2337 | Buffer.prototype.writeInt8 = function(value, offset, noAssert) { 2338 | var buffer = this; 2339 | 2340 | if (!noAssert) { 2341 | assert.ok(value !== undefined && value !== null, 2342 | 'missing value'); 2343 | 2344 | assert.ok(offset !== undefined && offset !== null, 2345 | 'missing offset'); 2346 | 2347 | assert.ok(offset < buffer.length, 2348 | 'Trying to write beyond buffer length'); 2349 | 2350 | verifsint(value, 0x7f, -0x80); 2351 | } 2352 | 2353 | if (value >= 0) { 2354 | buffer.writeUInt8(value, offset, noAssert); 2355 | } else { 2356 | buffer.writeUInt8(0xff + value + 1, offset, noAssert); 2357 | } 2358 | }; 2359 | 2360 | function writeInt16(buffer, value, offset, isBigEndian, noAssert) { 2361 | if (!noAssert) { 2362 | assert.ok(value !== undefined && value !== null, 2363 | 'missing value'); 2364 | 2365 | assert.ok(typeof (isBigEndian) === 'boolean', 2366 | 'missing or invalid endian'); 2367 | 2368 | assert.ok(offset !== undefined && offset !== null, 2369 | 'missing offset'); 2370 | 2371 | assert.ok(offset + 1 < buffer.length, 2372 | 'Trying to write beyond buffer length'); 2373 | 2374 | verifsint(value, 0x7fff, -0x8000); 2375 | } 2376 | 2377 | if (value >= 0) { 2378 | writeUInt16(buffer, value, offset, isBigEndian, noAssert); 2379 | } else { 2380 | writeUInt16(buffer, 0xffff + value + 1, offset, isBigEndian, noAssert); 2381 | } 2382 | } 2383 | 2384 | Buffer.prototype.writeInt16LE = function(value, offset, noAssert) { 2385 | writeInt16(this, value, offset, false, noAssert); 2386 | }; 2387 | 2388 | Buffer.prototype.writeInt16BE = function(value, offset, noAssert) { 2389 | writeInt16(this, value, offset, true, noAssert); 2390 | }; 2391 | 2392 | function writeInt32(buffer, value, offset, isBigEndian, noAssert) { 2393 | if (!noAssert) { 2394 | assert.ok(value !== undefined && value !== null, 2395 | 'missing value'); 2396 | 2397 | assert.ok(typeof (isBigEndian) === 'boolean', 2398 | 'missing or invalid endian'); 2399 | 2400 | assert.ok(offset !== undefined && offset !== null, 2401 | 'missing offset'); 2402 | 2403 | assert.ok(offset + 3 < buffer.length, 2404 | 'Trying to write beyond buffer length'); 2405 | 2406 | verifsint(value, 0x7fffffff, -0x80000000); 2407 | } 2408 | 2409 | if (value >= 0) { 2410 | writeUInt32(buffer, value, offset, isBigEndian, noAssert); 2411 | } else { 2412 | writeUInt32(buffer, 0xffffffff + value + 1, offset, isBigEndian, noAssert); 2413 | } 2414 | } 2415 | 2416 | Buffer.prototype.writeInt32LE = function(value, offset, noAssert) { 2417 | writeInt32(this, value, offset, false, noAssert); 2418 | }; 2419 | 2420 | Buffer.prototype.writeInt32BE = function(value, offset, noAssert) { 2421 | writeInt32(this, value, offset, true, noAssert); 2422 | }; 2423 | 2424 | function writeFloat(buffer, value, offset, isBigEndian, noAssert) { 2425 | if (!noAssert) { 2426 | assert.ok(value !== undefined && value !== null, 2427 | 'missing value'); 2428 | 2429 | assert.ok(typeof (isBigEndian) === 'boolean', 2430 | 'missing or invalid endian'); 2431 | 2432 | assert.ok(offset !== undefined && offset !== null, 2433 | 'missing offset'); 2434 | 2435 | assert.ok(offset + 3 < buffer.length, 2436 | 'Trying to write beyond buffer length'); 2437 | 2438 | verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38); 2439 | } 2440 | 2441 | require('./buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian, 2442 | 23, 4); 2443 | } 2444 | 2445 | Buffer.prototype.writeFloatLE = function(value, offset, noAssert) { 2446 | writeFloat(this, value, offset, false, noAssert); 2447 | }; 2448 | 2449 | Buffer.prototype.writeFloatBE = function(value, offset, noAssert) { 2450 | writeFloat(this, value, offset, true, noAssert); 2451 | }; 2452 | 2453 | function writeDouble(buffer, value, offset, isBigEndian, noAssert) { 2454 | if (!noAssert) { 2455 | assert.ok(value !== undefined && value !== null, 2456 | 'missing value'); 2457 | 2458 | assert.ok(typeof (isBigEndian) === 'boolean', 2459 | 'missing or invalid endian'); 2460 | 2461 | assert.ok(offset !== undefined && offset !== null, 2462 | 'missing offset'); 2463 | 2464 | assert.ok(offset + 7 < buffer.length, 2465 | 'Trying to write beyond buffer length'); 2466 | 2467 | verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308); 2468 | } 2469 | 2470 | require('./buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian, 2471 | 52, 8); 2472 | } 2473 | 2474 | Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) { 2475 | writeDouble(this, value, offset, false, noAssert); 2476 | }; 2477 | 2478 | Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) { 2479 | writeDouble(this, value, offset, true, noAssert); 2480 | }; 2481 | 2482 | SlowBuffer.prototype.readUInt8 = Buffer.prototype.readUInt8; 2483 | SlowBuffer.prototype.readUInt16LE = Buffer.prototype.readUInt16LE; 2484 | SlowBuffer.prototype.readUInt16BE = Buffer.prototype.readUInt16BE; 2485 | SlowBuffer.prototype.readUInt32LE = Buffer.prototype.readUInt32LE; 2486 | SlowBuffer.prototype.readUInt32BE = Buffer.prototype.readUInt32BE; 2487 | SlowBuffer.prototype.readInt8 = Buffer.prototype.readInt8; 2488 | SlowBuffer.prototype.readInt16LE = Buffer.prototype.readInt16LE; 2489 | SlowBuffer.prototype.readInt16BE = Buffer.prototype.readInt16BE; 2490 | SlowBuffer.prototype.readInt32LE = Buffer.prototype.readInt32LE; 2491 | SlowBuffer.prototype.readInt32BE = Buffer.prototype.readInt32BE; 2492 | SlowBuffer.prototype.readFloatLE = Buffer.prototype.readFloatLE; 2493 | SlowBuffer.prototype.readFloatBE = Buffer.prototype.readFloatBE; 2494 | SlowBuffer.prototype.readDoubleLE = Buffer.prototype.readDoubleLE; 2495 | SlowBuffer.prototype.readDoubleBE = Buffer.prototype.readDoubleBE; 2496 | SlowBuffer.prototype.writeUInt8 = Buffer.prototype.writeUInt8; 2497 | SlowBuffer.prototype.writeUInt16LE = Buffer.prototype.writeUInt16LE; 2498 | SlowBuffer.prototype.writeUInt16BE = Buffer.prototype.writeUInt16BE; 2499 | SlowBuffer.prototype.writeUInt32LE = Buffer.prototype.writeUInt32LE; 2500 | SlowBuffer.prototype.writeUInt32BE = Buffer.prototype.writeUInt32BE; 2501 | SlowBuffer.prototype.writeInt8 = Buffer.prototype.writeInt8; 2502 | SlowBuffer.prototype.writeInt16LE = Buffer.prototype.writeInt16LE; 2503 | SlowBuffer.prototype.writeInt16BE = Buffer.prototype.writeInt16BE; 2504 | SlowBuffer.prototype.writeInt32LE = Buffer.prototype.writeInt32LE; 2505 | SlowBuffer.prototype.writeInt32BE = Buffer.prototype.writeInt32BE; 2506 | SlowBuffer.prototype.writeFloatLE = Buffer.prototype.writeFloatLE; 2507 | SlowBuffer.prototype.writeFloatBE = Buffer.prototype.writeFloatBE; 2508 | SlowBuffer.prototype.writeDoubleLE = Buffer.prototype.writeDoubleLE; 2509 | SlowBuffer.prototype.writeDoubleBE = Buffer.prototype.writeDoubleBE; 2510 | }); 2511 | 2512 | require.define("/node_modules/buffer-browserify/node_modules/base64-js/package.json",function(require,module,exports,__dirname,__filename,process){module.exports = {"main":"lib/b64.js"}}); 2513 | 2514 | require.define("/node_modules/buffer-browserify/node_modules/base64-js/lib/b64.js",function(require,module,exports,__dirname,__filename,process){(function (exports) { 2515 | 'use strict'; 2516 | 2517 | var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; 2518 | 2519 | function b64ToByteArray(b64) { 2520 | var i, j, l, tmp, placeHolders, arr; 2521 | 2522 | if (b64.length % 4 > 0) { 2523 | throw 'Invalid string. Length must be a multiple of 4'; 2524 | } 2525 | 2526 | // the number of equal signs (place holders) 2527 | // if there are two placeholders, than the two characters before it 2528 | // represent one byte 2529 | // if there is only one, then the three characters before it represent 2 bytes 2530 | // this is just a cheap hack to not do indexOf twice 2531 | placeHolders = b64.indexOf('='); 2532 | placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0; 2533 | 2534 | // base64 is 4/3 + up to two characters of the original data 2535 | arr = [];//new Uint8Array(b64.length * 3 / 4 - placeHolders); 2536 | 2537 | // if there are placeholders, only get up to the last complete 4 chars 2538 | l = placeHolders > 0 ? b64.length - 4 : b64.length; 2539 | 2540 | for (i = 0, j = 0; i < l; i += 4, j += 3) { 2541 | tmp = (lookup.indexOf(b64[i]) << 18) | (lookup.indexOf(b64[i + 1]) << 12) | (lookup.indexOf(b64[i + 2]) << 6) | lookup.indexOf(b64[i + 3]); 2542 | arr.push((tmp & 0xFF0000) >> 16); 2543 | arr.push((tmp & 0xFF00) >> 8); 2544 | arr.push(tmp & 0xFF); 2545 | } 2546 | 2547 | if (placeHolders === 2) { 2548 | tmp = (lookup.indexOf(b64[i]) << 2) | (lookup.indexOf(b64[i + 1]) >> 4); 2549 | arr.push(tmp & 0xFF); 2550 | } else if (placeHolders === 1) { 2551 | tmp = (lookup.indexOf(b64[i]) << 10) | (lookup.indexOf(b64[i + 1]) << 4) | (lookup.indexOf(b64[i + 2]) >> 2); 2552 | arr.push((tmp >> 8) & 0xFF); 2553 | arr.push(tmp & 0xFF); 2554 | } 2555 | 2556 | return arr; 2557 | } 2558 | 2559 | function uint8ToBase64(uint8) { 2560 | var i, 2561 | extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes 2562 | output = "", 2563 | temp, length; 2564 | 2565 | function tripletToBase64 (num) { 2566 | return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]; 2567 | }; 2568 | 2569 | // go through the array every three bytes, we'll deal with trailing stuff later 2570 | for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) { 2571 | temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]); 2572 | output += tripletToBase64(temp); 2573 | } 2574 | 2575 | // pad the end with zeros, but make sure to not forget the extra bytes 2576 | switch (extraBytes) { 2577 | case 1: 2578 | temp = uint8[uint8.length - 1]; 2579 | output += lookup[temp >> 2]; 2580 | output += lookup[(temp << 4) & 0x3F]; 2581 | output += '=='; 2582 | break; 2583 | case 2: 2584 | temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]); 2585 | output += lookup[temp >> 10]; 2586 | output += lookup[(temp >> 4) & 0x3F]; 2587 | output += lookup[(temp << 2) & 0x3F]; 2588 | output += '='; 2589 | break; 2590 | } 2591 | 2592 | return output; 2593 | } 2594 | 2595 | module.exports.toByteArray = b64ToByteArray; 2596 | module.exports.fromByteArray = uint8ToBase64; 2597 | }()); 2598 | }); 2599 | 2600 | require.define("/node_modules/buffer-browserify/buffer_ieee754.js",function(require,module,exports,__dirname,__filename,process){exports.readIEEE754 = function(buffer, offset, isBE, mLen, nBytes) { 2601 | var e, m, 2602 | eLen = nBytes * 8 - mLen - 1, 2603 | eMax = (1 << eLen) - 1, 2604 | eBias = eMax >> 1, 2605 | nBits = -7, 2606 | i = isBE ? 0 : (nBytes - 1), 2607 | d = isBE ? 1 : -1, 2608 | s = buffer[offset + i]; 2609 | 2610 | i += d; 2611 | 2612 | e = s & ((1 << (-nBits)) - 1); 2613 | s >>= (-nBits); 2614 | nBits += eLen; 2615 | for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8); 2616 | 2617 | m = e & ((1 << (-nBits)) - 1); 2618 | e >>= (-nBits); 2619 | nBits += mLen; 2620 | for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8); 2621 | 2622 | if (e === 0) { 2623 | e = 1 - eBias; 2624 | } else if (e === eMax) { 2625 | return m ? NaN : ((s ? -1 : 1) * Infinity); 2626 | } else { 2627 | m = m + Math.pow(2, mLen); 2628 | e = e - eBias; 2629 | } 2630 | return (s ? -1 : 1) * m * Math.pow(2, e - mLen); 2631 | }; 2632 | 2633 | exports.writeIEEE754 = function(buffer, value, offset, isBE, mLen, nBytes) { 2634 | var e, m, c, 2635 | eLen = nBytes * 8 - mLen - 1, 2636 | eMax = (1 << eLen) - 1, 2637 | eBias = eMax >> 1, 2638 | rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0), 2639 | i = isBE ? (nBytes - 1) : 0, 2640 | d = isBE ? -1 : 1, 2641 | s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0; 2642 | 2643 | value = Math.abs(value); 2644 | 2645 | if (isNaN(value) || value === Infinity) { 2646 | m = isNaN(value) ? 1 : 0; 2647 | e = eMax; 2648 | } else { 2649 | e = Math.floor(Math.log(value) / Math.LN2); 2650 | if (value * (c = Math.pow(2, -e)) < 1) { 2651 | e--; 2652 | c *= 2; 2653 | } 2654 | if (e + eBias >= 1) { 2655 | value += rt / c; 2656 | } else { 2657 | value += rt * Math.pow(2, 1 - eBias); 2658 | } 2659 | if (value * c >= 2) { 2660 | e++; 2661 | c /= 2; 2662 | } 2663 | 2664 | if (e + eBias >= eMax) { 2665 | m = 0; 2666 | e = eMax; 2667 | } else if (e + eBias >= 1) { 2668 | m = (value * c - 1) * Math.pow(2, mLen); 2669 | e = e + eBias; 2670 | } else { 2671 | m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen); 2672 | e = 0; 2673 | } 2674 | } 2675 | 2676 | for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8); 2677 | 2678 | e = (e << mLen) | m; 2679 | eLen += mLen; 2680 | for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8); 2681 | 2682 | buffer[offset + i - d] |= s * 128; 2683 | }; 2684 | }); 2685 | 2686 | require.define("/foo.js",function(require,module,exports,__dirname,__filename,process){assert = require('assert') 2687 | }); 2688 | require("/foo.js"); 2689 | })(); --------------------------------------------------------------------------------