├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── examples ├── Gulpfile.js ├── build │ └── normal.js └── src │ ├── index.js │ └── normal.js ├── index.js ├── package.json └── test ├── expected └── .keep ├── fixtures ├── add.js ├── add_file.js ├── bar.js ├── custom_resolved.js ├── exclude.js ├── ext_bar.bar ├── ext_foo.foo ├── extension.js ├── increment.js ├── normal.js ├── normal2.js ├── shim.js ├── trans_bar.coffee ├── trans_error.coffee ├── trans_foo.coffee └── transform.coffee ├── main.js └── prepare.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | .jshintrc 4 | .editorconfig 5 | test/expected/*.js 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.9 4 | - 0.10 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Robo (deepak1556) https://github.com/deepak1556 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## NOTE: THIS PLUGIN IS NO LONGER MAINTAINED , checkout [gulp-bro](https://github.com/ngryman/gulp-bro) for a similar plugin, or the [recipes](https://github.com/gulpjs/gulp/tree/master/docs/recipes) by gulp team for reference on using browserify with gulp. 2 | 3 | [![Build Status](https://travis-ci.org/deepak1556/gulp-browserify.png)](https://travis-ci.org/deepak1556/gulp-browserify) 4 | [![NPM version](https://badge.fury.io/js/gulp-browserify.png)](http://badge.fury.io/js/gulp-browserify) 5 | 6 | # [gulp](https://github.com/gulpjs/gulp)-browserify 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
Packagegulp-browserify
DescriptionBundle modules with BrowserifyJS
Node Version>= 0.9
Gulp Version3.x
26 | 27 | # Usage 28 | 29 | 30 | ## Install 31 | 32 | ``` 33 | npm install --save-dev gulp-browserify 34 | ``` 35 | 36 | ## Example 37 | 38 | ```javascript 39 | var gulp = require('gulp'); 40 | var browserify = require('gulp-browserify'); 41 | 42 | // Basic usage 43 | gulp.task('scripts', function() { 44 | // Single entry point to browserify 45 | gulp.src('src/js/app.js') 46 | .pipe(browserify({ 47 | insertGlobals : true, 48 | debug : !gulp.env.production 49 | })) 50 | .pipe(gulp.dest('./build/js')) 51 | }); 52 | ``` 53 | 54 | Make sure to pipe *only entry points*. Browserify will take care of other dependencies for you. 55 | 56 | ### Options 57 | 58 | #### transform 59 | 60 | Type : `[String || function]` 61 | 62 | Specifies a pipeline of functions (or module names) through which the browserified bundle will be run. Check out [the list of transforms on node-browserify](https://github.com/substack/node-browserify#list-of-source-transforms). 63 | 64 | ##### Languages that compile to JavaScript 65 | 66 | If you want to bundle files with extensions other than `.js` or `.json`, omit contents from streamed files and set `extensions` option. 67 | 68 | Let's say you want to browserify CoffeeScript, install `coffeeify` and: 69 | 70 | ```javascript 71 | var gulp = require('gulp'); 72 | var browserify = require('gulp-browserify'); 73 | var rename = require('gulp-rename'); 74 | 75 | gulp.task('coffee', function() { 76 | gulp.src('src/coffee/app.coffee', { read: false }) 77 | .pipe(browserify({ 78 | transform: ['coffeeify'], 79 | extensions: ['.coffee'] 80 | })) 81 | .pipe(rename('app.js')) 82 | .pipe(gulp.dest('./build/js')) 83 | }); 84 | ``` 85 | 86 | If you forget `{ read: false }`, gulp-browserify will passes the contents stream of a incoming file to node-browserify. Then node-browserify names the stream as `fake_xxx.js` and process it. Some transforms such as `coffeeify` determines whether to transform files with extensions. That is why you need `{ read: false }` for AltJS. 87 | 88 | #### debug 89 | 90 | Type : `Boolean` 91 | 92 | Enable source map support. `!gulp.env.production` would work well. 93 | 94 | #### extensions 95 | 96 | Type: `[String]` 97 | 98 | Array of extensions that you want to skip in `require()` calls in addition to `.js` and `.json`. Don't forget `.`. 99 | 100 | With `{ extensions: ['.coffee'] }`, you can't do `require('app')`. Instead, you have to do `require('app.coffee')`. 101 | 102 | #### ignore 103 | Type: `[String]` 104 | 105 | Array of paths which should be passed to the ignore function of 106 | browserify. 107 | 108 | #### resolve 109 | 110 | Type: `Function` 111 | 112 | Custom module name resolution function. From [node-browserify](https://github.com/substack/node-browserify#var-b--browserifyfiles-or-opts) documentation: 113 | > You can give browserify a custom `opts.resolve()` function or by default it uses 114 | [`browser-resolve`](https://npmjs.org/package/browser-resolve). 115 | 116 | Obviously, this function must implement the same API as [browser-resolve](https://npmjs.org/package/browser-resolve). 117 | 118 | #### Other Options 119 | 120 | Any other options you provide will be passed through to browserify. This is useful for setting things like `standalone` or `ignoreGlobals`. 121 | 122 | ### Custom options 123 | 124 | #### nobuiltins 125 | 126 | Remove builtins modules defined in `lib/builtins.js` (browserify module). 127 | `opts.builtins` must be not defined and `opts.nobuiltins` can be an Array of 128 | Strings or simply a String. 129 | 130 | ```js 131 | gulp.task('scripts', function() { 132 | gulp.src(['src/index.js']) 133 | .pipe(browserify({ 134 | nobuiltins: 'events querystring' 135 | })) 136 | .pipe(gulp.dest('./build/js')) 137 | }); 138 | ``` 139 | 140 | ### Browserify-Shim 141 | 142 | Example configuration 143 | 144 | ```javascript 145 | gulp.task('scripts', function() { 146 | //single entry point to browserify 147 | gulp.src(['src/index.js']) 148 | .pipe(browserify({ 149 | shim: { 150 | angular: { 151 | path: '/vendor/angular/angular.js', 152 | exports: 'angular' 153 | }, 154 | 'angular-route': { 155 | path: '/vendor/angular-route/angular-route.js', 156 | exports: 'ngRoute', 157 | depends: { 158 | angular: 'angular' 159 | } 160 | } 161 | } 162 | })) 163 | .pipe(concat('dest.js')) 164 | .pipe(gulp.dest('./build')) 165 | }); 166 | ``` 167 | More information about configuring browserify-shim can be found [here](https://github.com/thlorenz/browserify-shim/blob/97d416cb3bc2ef531fae05a8eed4c86700ba4dc8/README.md). 168 | 169 | ### Events 170 | 171 | Other than standard Node.js stream events, gulp-browserify emits its own events. 172 | 173 | #### prebundle 174 | 175 | ```javascript 176 | .on('prebundle', function(bundler){}) 177 | ``` 178 | 179 | Event triggered just before invoking `bundler.bundle()` and provides the bundler object to work with in the callback. 180 | 181 | This is especially useful if you want to `require()`, `external()` or other methods of node-browserify. 182 | 183 | ```javascript 184 | gulp.task('scripts', function() { 185 | gulp.src('src/js/app.js') 186 | .pipe(browserify({ 187 | insertGlobals : true, 188 | debug : !gulp.env.production 189 | })) 190 | .on('prebundle', function(bundle) { 191 | bundle.external('domready'); 192 | bundle.external('react'); 193 | }) 194 | .pipe(gulp.dest('./build/js')) 195 | }); 196 | ``` 197 | 198 | #### postbundle 199 | 200 | ```javascript 201 | .on('postbundle', function(src){}) 202 | ``` 203 | 204 | Event triggered after the bundle process is over and provides the bundled data as an argument to the callback. 205 | 206 | 207 | 208 | # License 209 | 210 | Copyright (c) 2014 Robo (deepak1556) https://github.com/deepak1556 211 | 212 | Permission is hereby granted, free of charge, to any person obtaining 213 | a copy of this software and associated documentation files (the 214 | "Software"), to deal in the Software without restriction, including 215 | without limitation the rights to use, copy, modify, merge, publish, 216 | distribute, sublicense, and/or sell copies of the Software, and to 217 | permit persons to whom the Software is furnished to do so, subject to 218 | the following conditions: 219 | 220 | The above copyright notice and this permission notice shall be 221 | included in all copies or substantial portions of the Software. 222 | 223 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 224 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 225 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 226 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 227 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 228 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 229 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 230 | -------------------------------------------------------------------------------- /examples/Gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'); 2 | var browserify = require('../'); 3 | 4 | gulp.task('scripts', function(){ 5 | gulp.src('../test/fixtures/normal.js') 6 | .pipe(browserify()) 7 | .pipe(gulp.dest('./build')); 8 | }); 9 | 10 | gulp.task('bundle', function(){ 11 | gulp.src('./src/*.js') 12 | .pipe(browserify()) 13 | .pipe(gulp.dest('./bundle')); 14 | }); 15 | 16 | gulp.task('default', function() { 17 | gulp.run('scripts'); 18 | }); -------------------------------------------------------------------------------- /examples/build/normal.js: -------------------------------------------------------------------------------- 1 | (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 10 | // 11 | // Permission is hereby granted, free of charge, to any person obtaining a copy 12 | // of this software and associated documentation files (the 'Software'), to 13 | // deal in the Software without restriction, including without limitation the 14 | // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 15 | // sell copies of the Software, and to permit persons to whom the Software is 16 | // furnished to do so, subject to the following conditions: 17 | // 18 | // The above copyright notice and this permission notice shall be included in 19 | // all copies or substantial portions of the Software. 20 | // 21 | // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 | // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 25 | // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 26 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 | 28 | // when used in node, this will actually load the util module we depend on 29 | // versus loading the builtin util module as happens otherwise 30 | // this is a bug in node module loading as far as I am concerned 31 | var util = require('util/'); 32 | 33 | var pSlice = Array.prototype.slice; 34 | var hasOwn = Object.prototype.hasOwnProperty; 35 | 36 | // 1. The assert module provides functions that throw 37 | // AssertionError's when particular conditions are not met. The 38 | // assert module must conform to the following interface. 39 | 40 | var assert = module.exports = ok; 41 | 42 | // 2. The AssertionError is defined in assert. 43 | // new assert.AssertionError({ message: message, 44 | // actual: actual, 45 | // expected: expected }) 46 | 47 | assert.AssertionError = function AssertionError(options) { 48 | this.name = 'AssertionError'; 49 | this.actual = options.actual; 50 | this.expected = options.expected; 51 | this.operator = options.operator; 52 | if (options.message) { 53 | this.message = options.message; 54 | this.generatedMessage = false; 55 | } else { 56 | this.message = getMessage(this); 57 | this.generatedMessage = true; 58 | } 59 | var stackStartFunction = options.stackStartFunction || fail; 60 | 61 | if (Error.captureStackTrace) { 62 | Error.captureStackTrace(this, stackStartFunction); 63 | } 64 | }; 65 | 66 | // assert.AssertionError instanceof Error 67 | util.inherits(assert.AssertionError, Error); 68 | 69 | function replacer(key, value) { 70 | if (util.isUndefined(value)) { 71 | return '' + value; 72 | } 73 | if (util.isNumber(value) && (isNaN(value) || !isFinite(value))) { 74 | return value.toString(); 75 | } 76 | if (util.isFunction(value) || util.isRegExp(value)) { 77 | return value.toString(); 78 | } 79 | return value; 80 | } 81 | 82 | function truncate(s, n) { 83 | if (util.isString(s)) { 84 | return s.length < n ? s : s.slice(0, n); 85 | } else { 86 | return s; 87 | } 88 | } 89 | 90 | function getMessage(self) { 91 | return truncate(JSON.stringify(self.actual, replacer), 128) + ' ' + 92 | self.operator + ' ' + 93 | truncate(JSON.stringify(self.expected, replacer), 128); 94 | } 95 | 96 | // At present only the three keys mentioned above are used and 97 | // understood by the spec. Implementations or sub modules can pass 98 | // other keys to the AssertionError's constructor - they will be 99 | // ignored. 100 | 101 | // 3. All of the following functions must throw an AssertionError 102 | // when a corresponding condition is not met, with a message that 103 | // may be undefined if not provided. All assertion methods provide 104 | // both the actual and expected values to the assertion error for 105 | // display purposes. 106 | 107 | function fail(actual, expected, message, operator, stackStartFunction) { 108 | throw new assert.AssertionError({ 109 | message: message, 110 | actual: actual, 111 | expected: expected, 112 | operator: operator, 113 | stackStartFunction: stackStartFunction 114 | }); 115 | } 116 | 117 | // EXTENSION! allows for well behaved errors defined elsewhere. 118 | assert.fail = fail; 119 | 120 | // 4. Pure assertion tests whether a value is truthy, as determined 121 | // by !!guard. 122 | // assert.ok(guard, message_opt); 123 | // This statement is equivalent to assert.equal(true, !!guard, 124 | // message_opt);. To test strictly for the value true, use 125 | // assert.strictEqual(true, guard, message_opt);. 126 | 127 | function ok(value, message) { 128 | if (!value) fail(value, true, message, '==', assert.ok); 129 | } 130 | assert.ok = ok; 131 | 132 | // 5. The equality assertion tests shallow, coercive equality with 133 | // ==. 134 | // assert.equal(actual, expected, message_opt); 135 | 136 | assert.equal = function equal(actual, expected, message) { 137 | if (actual != expected) fail(actual, expected, message, '==', assert.equal); 138 | }; 139 | 140 | // 6. The non-equality assertion tests for whether two objects are not equal 141 | // with != assert.notEqual(actual, expected, message_opt); 142 | 143 | assert.notEqual = function notEqual(actual, expected, message) { 144 | if (actual == expected) { 145 | fail(actual, expected, message, '!=', assert.notEqual); 146 | } 147 | }; 148 | 149 | // 7. The equivalence assertion tests a deep equality relation. 150 | // assert.deepEqual(actual, expected, message_opt); 151 | 152 | assert.deepEqual = function deepEqual(actual, expected, message) { 153 | if (!_deepEqual(actual, expected)) { 154 | fail(actual, expected, message, 'deepEqual', assert.deepEqual); 155 | } 156 | }; 157 | 158 | function _deepEqual(actual, expected) { 159 | // 7.1. All identical values are equivalent, as determined by ===. 160 | if (actual === expected) { 161 | return true; 162 | 163 | } else if (util.isBuffer(actual) && util.isBuffer(expected)) { 164 | if (actual.length != expected.length) return false; 165 | 166 | for (var i = 0; i < actual.length; i++) { 167 | if (actual[i] !== expected[i]) return false; 168 | } 169 | 170 | return true; 171 | 172 | // 7.2. If the expected value is a Date object, the actual value is 173 | // equivalent if it is also a Date object that refers to the same time. 174 | } else if (util.isDate(actual) && util.isDate(expected)) { 175 | return actual.getTime() === expected.getTime(); 176 | 177 | // 7.3 If the expected value is a RegExp object, the actual value is 178 | // equivalent if it is also a RegExp object with the same source and 179 | // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`). 180 | } else if (util.isRegExp(actual) && util.isRegExp(expected)) { 181 | return actual.source === expected.source && 182 | actual.global === expected.global && 183 | actual.multiline === expected.multiline && 184 | actual.lastIndex === expected.lastIndex && 185 | actual.ignoreCase === expected.ignoreCase; 186 | 187 | // 7.4. Other pairs that do not both pass typeof value == 'object', 188 | // equivalence is determined by ==. 189 | } else if (!util.isObject(actual) && !util.isObject(expected)) { 190 | return actual == expected; 191 | 192 | // 7.5 For all other Object pairs, including Array objects, equivalence is 193 | // determined by having the same number of owned properties (as verified 194 | // with Object.prototype.hasOwnProperty.call), the same set of keys 195 | // (although not necessarily the same order), equivalent values for every 196 | // corresponding key, and an identical 'prototype' property. Note: this 197 | // accounts for both named and indexed properties on Arrays. 198 | } else { 199 | return objEquiv(actual, expected); 200 | } 201 | } 202 | 203 | function isArguments(object) { 204 | return Object.prototype.toString.call(object) == '[object Arguments]'; 205 | } 206 | 207 | function objEquiv(a, b) { 208 | if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b)) 209 | return false; 210 | // an identical 'prototype' property. 211 | if (a.prototype !== b.prototype) return false; 212 | //~~~I've managed to break Object.keys through screwy arguments passing. 213 | // Converting to array solves the problem. 214 | if (isArguments(a)) { 215 | if (!isArguments(b)) { 216 | return false; 217 | } 218 | a = pSlice.call(a); 219 | b = pSlice.call(b); 220 | return _deepEqual(a, b); 221 | } 222 | try { 223 | var ka = objectKeys(a), 224 | kb = objectKeys(b), 225 | key, i; 226 | } catch (e) {//happens when one is a string literal and the other isn't 227 | return false; 228 | } 229 | // having the same number of owned properties (keys incorporates 230 | // hasOwnProperty) 231 | if (ka.length != kb.length) 232 | return false; 233 | //the same set of keys (although not necessarily the same order), 234 | ka.sort(); 235 | kb.sort(); 236 | //~~~cheap key test 237 | for (i = ka.length - 1; i >= 0; i--) { 238 | if (ka[i] != kb[i]) 239 | return false; 240 | } 241 | //equivalent values for every corresponding key, and 242 | //~~~possibly expensive deep test 243 | for (i = ka.length - 1; i >= 0; i--) { 244 | key = ka[i]; 245 | if (!_deepEqual(a[key], b[key])) return false; 246 | } 247 | return true; 248 | } 249 | 250 | // 8. The non-equivalence assertion tests for any deep inequality. 251 | // assert.notDeepEqual(actual, expected, message_opt); 252 | 253 | assert.notDeepEqual = function notDeepEqual(actual, expected, message) { 254 | if (_deepEqual(actual, expected)) { 255 | fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); 256 | } 257 | }; 258 | 259 | // 9. The strict equality assertion tests strict equality, as determined by ===. 260 | // assert.strictEqual(actual, expected, message_opt); 261 | 262 | assert.strictEqual = function strictEqual(actual, expected, message) { 263 | if (actual !== expected) { 264 | fail(actual, expected, message, '===', assert.strictEqual); 265 | } 266 | }; 267 | 268 | // 10. The strict non-equality assertion tests for strict inequality, as 269 | // determined by !==. assert.notStrictEqual(actual, expected, message_opt); 270 | 271 | assert.notStrictEqual = function notStrictEqual(actual, expected, message) { 272 | if (actual === expected) { 273 | fail(actual, expected, message, '!==', assert.notStrictEqual); 274 | } 275 | }; 276 | 277 | function expectedException(actual, expected) { 278 | if (!actual || !expected) { 279 | return false; 280 | } 281 | 282 | if (Object.prototype.toString.call(expected) == '[object RegExp]') { 283 | return expected.test(actual); 284 | } else if (actual instanceof expected) { 285 | return true; 286 | } else if (expected.call({}, actual) === true) { 287 | return true; 288 | } 289 | 290 | return false; 291 | } 292 | 293 | function _throws(shouldThrow, block, expected, message) { 294 | var actual; 295 | 296 | if (util.isString(expected)) { 297 | message = expected; 298 | expected = null; 299 | } 300 | 301 | try { 302 | block(); 303 | } catch (e) { 304 | actual = e; 305 | } 306 | 307 | message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + 308 | (message ? ' ' + message : '.'); 309 | 310 | if (shouldThrow && !actual) { 311 | fail(actual, expected, 'Missing expected exception' + message); 312 | } 313 | 314 | if (!shouldThrow && expectedException(actual, expected)) { 315 | fail(actual, expected, 'Got unwanted exception' + message); 316 | } 317 | 318 | if ((shouldThrow && actual && expected && 319 | !expectedException(actual, expected)) || (!shouldThrow && actual)) { 320 | throw actual; 321 | } 322 | } 323 | 324 | // 11. Expected to throw an error: 325 | // assert.throws(block, Error_opt, message_opt); 326 | 327 | assert.throws = function(block, /*optional*/error, /*optional*/message) { 328 | _throws.apply(this, [true].concat(pSlice.call(arguments))); 329 | }; 330 | 331 | // EXTENSION! This is annoying to write outside this module. 332 | assert.doesNotThrow = function(block, /*optional*/message) { 333 | _throws.apply(this, [false].concat(pSlice.call(arguments))); 334 | }; 335 | 336 | assert.ifError = function(err) { if (err) {throw err;}}; 337 | 338 | var objectKeys = Object.keys || function (obj) { 339 | var keys = []; 340 | for (var key in obj) { 341 | if (hasOwn.call(obj, key)) keys.push(key); 342 | } 343 | return keys; 344 | }; 345 | 346 | },{"util/":7}],3:[function(require,module,exports){ 347 | if (typeof Object.create === 'function') { 348 | // implementation from standard node.js 'util' module 349 | module.exports = function inherits(ctor, superCtor) { 350 | ctor.super_ = superCtor 351 | ctor.prototype = Object.create(superCtor.prototype, { 352 | constructor: { 353 | value: ctor, 354 | enumerable: false, 355 | writable: true, 356 | configurable: true 357 | } 358 | }); 359 | }; 360 | } else { 361 | // old school shim for old browsers 362 | module.exports = function inherits(ctor, superCtor) { 363 | ctor.super_ = superCtor 364 | var TempCtor = function () {} 365 | TempCtor.prototype = superCtor.prototype 366 | ctor.prototype = new TempCtor() 367 | ctor.prototype.constructor = ctor 368 | } 369 | } 370 | 371 | },{}],4:[function(require,module,exports){ 372 | require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o remaining) { 538 | length = remaining 539 | } 540 | } 541 | 542 | // must be an even number of digits 543 | var strLen = string.length 544 | if (strLen % 2 !== 0) { 545 | throw new Error('Invalid hex string') 546 | } 547 | if (length > strLen / 2) { 548 | length = strLen / 2 549 | } 550 | for (var i = 0; i < length; i++) { 551 | var byte = parseInt(string.substr(i * 2, 2), 16) 552 | if (isNaN(byte)) throw new Error('Invalid hex string') 553 | buf[offset + i] = byte 554 | } 555 | Buffer._charsWritten = i * 2 556 | return i 557 | } 558 | 559 | function _utf8Write (buf, string, offset, length) { 560 | var bytes, pos 561 | return Buffer._charsWritten = blitBuffer(utf8ToBytes(string), buf, offset, length) 562 | } 563 | 564 | function _asciiWrite (buf, string, offset, length) { 565 | var bytes, pos 566 | return Buffer._charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length) 567 | } 568 | 569 | function _binaryWrite (buf, string, offset, length) { 570 | return _asciiWrite(buf, string, offset, length) 571 | } 572 | 573 | function _base64Write (buf, string, offset, length) { 574 | var bytes, pos 575 | return Buffer._charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length) 576 | } 577 | 578 | function BufferWrite (string, offset, length, encoding) { 579 | // Support both (string, offset, length, encoding) 580 | // and the legacy (string, encoding, offset, length) 581 | if (isFinite(offset)) { 582 | if (!isFinite(length)) { 583 | encoding = length 584 | length = undefined 585 | } 586 | } else { // legacy 587 | var swap = encoding 588 | encoding = offset 589 | offset = length 590 | length = swap 591 | } 592 | 593 | offset = Number(offset) || 0 594 | var remaining = this.length - offset 595 | if (!length) { 596 | length = remaining 597 | } else { 598 | length = Number(length) 599 | if (length > remaining) { 600 | length = remaining 601 | } 602 | } 603 | encoding = String(encoding || 'utf8').toLowerCase() 604 | 605 | switch (encoding) { 606 | case 'hex': 607 | return _hexWrite(this, string, offset, length) 608 | 609 | case 'utf8': 610 | case 'utf-8': 611 | return _utf8Write(this, string, offset, length) 612 | 613 | case 'ascii': 614 | return _asciiWrite(this, string, offset, length) 615 | 616 | case 'binary': 617 | return _binaryWrite(this, string, offset, length) 618 | 619 | case 'base64': 620 | return _base64Write(this, string, offset, length) 621 | 622 | default: 623 | throw new Error('Unknown encoding') 624 | } 625 | } 626 | 627 | function BufferToString (encoding, start, end) { 628 | var self = (this instanceof ProxyBuffer) 629 | ? this._proxy 630 | : this 631 | 632 | encoding = String(encoding || 'utf8').toLowerCase() 633 | start = Number(start) || 0 634 | end = (end !== undefined) 635 | ? Number(end) 636 | : end = self.length 637 | 638 | // Fastpath empty strings 639 | if (end === start) 640 | return '' 641 | 642 | switch (encoding) { 643 | case 'hex': 644 | return _hexSlice(self, start, end) 645 | 646 | case 'utf8': 647 | case 'utf-8': 648 | return _utf8Slice(self, start, end) 649 | 650 | case 'ascii': 651 | return _asciiSlice(self, start, end) 652 | 653 | case 'binary': 654 | return _binarySlice(self, start, end) 655 | 656 | case 'base64': 657 | return _base64Slice(self, start, end) 658 | 659 | default: 660 | throw new Error('Unknown encoding') 661 | } 662 | } 663 | 664 | function BufferToJSON () { 665 | return { 666 | type: 'Buffer', 667 | data: Array.prototype.slice.call(this, 0) 668 | } 669 | } 670 | 671 | // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) 672 | function BufferCopy (target, target_start, start, end) { 673 | var source = this 674 | 675 | if (!start) start = 0 676 | if (!end && end !== 0) end = this.length 677 | if (!target_start) target_start = 0 678 | 679 | // Copy 0 bytes; we're done 680 | if (end === start) return 681 | if (target.length === 0 || source.length === 0) return 682 | 683 | // Fatal error conditions 684 | if (end < start) 685 | throw new Error('sourceEnd < sourceStart') 686 | if (target_start < 0 || target_start >= target.length) 687 | throw new Error('targetStart out of bounds') 688 | if (start < 0 || start >= source.length) 689 | throw new Error('sourceStart out of bounds') 690 | if (end < 0 || end > source.length) 691 | throw new Error('sourceEnd out of bounds') 692 | 693 | // Are we oob? 694 | if (end > this.length) 695 | end = this.length 696 | if (target.length - target_start < end - start) 697 | end = target.length - target_start + start 698 | 699 | // copy! 700 | for (var i = 0; i < end - start; i++) 701 | target[i + target_start] = this[i + start] 702 | } 703 | 704 | function _base64Slice (buf, start, end) { 705 | var bytes = buf.slice(start, end) 706 | return require('base64-js').fromByteArray(bytes) 707 | } 708 | 709 | function _utf8Slice (buf, start, end) { 710 | var bytes = buf.slice(start, end) 711 | var res = '' 712 | var tmp = '' 713 | var i = 0 714 | while (i < bytes.length) { 715 | if (bytes[i] <= 0x7F) { 716 | res += decodeUtf8Char(tmp) + String.fromCharCode(bytes[i]) 717 | tmp = '' 718 | } else { 719 | tmp += '%' + bytes[i].toString(16) 720 | } 721 | 722 | i++ 723 | } 724 | 725 | return res + decodeUtf8Char(tmp) 726 | } 727 | 728 | function _asciiSlice (buf, start, end) { 729 | var bytes = buf.slice(start, end) 730 | var ret = '' 731 | for (var i = 0; i < bytes.length; i++) 732 | ret += String.fromCharCode(bytes[i]) 733 | return ret 734 | } 735 | 736 | function _binarySlice (buf, start, end) { 737 | return _asciiSlice(buf, start, end) 738 | } 739 | 740 | function _hexSlice (buf, start, end) { 741 | var len = buf.length 742 | 743 | if (!start || start < 0) start = 0 744 | if (!end || end < 0 || end > len) end = len 745 | 746 | var out = '' 747 | for (var i = start; i < end; i++) { 748 | out += toHex(buf[i]) 749 | } 750 | return out 751 | } 752 | 753 | // TODO: add test that modifying the new buffer slice will modify memory in the 754 | // original buffer! Use code from: 755 | // http://nodejs.org/api/buffer.html#buffer_buf_slice_start_end 756 | function BufferSlice (start, end) { 757 | var len = this.length 758 | start = clamp(start, len, 0) 759 | end = clamp(end, len, len) 760 | return augment(this.subarray(start, end)) // Uint8Array built-in method 761 | } 762 | 763 | function BufferReadUInt8 (offset, noAssert) { 764 | var buf = this 765 | if (!noAssert) { 766 | assert(offset !== undefined && offset !== null, 'missing offset') 767 | assert(offset < buf.length, 'Trying to read beyond buffer length') 768 | } 769 | 770 | if (offset >= buf.length) 771 | return 772 | 773 | return buf[offset] 774 | } 775 | 776 | function _readUInt16 (buf, offset, littleEndian, noAssert) { 777 | if (!noAssert) { 778 | assert(typeof (littleEndian) === 'boolean', 779 | 'missing or invalid endian') 780 | assert(offset !== undefined && offset !== null, 'missing offset') 781 | assert(offset + 1 < buf.length, 'Trying to read beyond buffer length') 782 | } 783 | 784 | var len = buf.length 785 | if (offset >= len) { 786 | return 787 | } else if (offset + 1 === len) { 788 | var dv = new xDataView(new xArrayBuffer(2)) 789 | dv.setUint8(0, buf[len - 1]) 790 | return dv.getUint16(0, littleEndian) 791 | } else { 792 | return buf._dataview.getUint16(offset, littleEndian) 793 | } 794 | } 795 | 796 | function BufferReadUInt16LE (offset, noAssert) { 797 | return _readUInt16(this, offset, true, noAssert) 798 | } 799 | 800 | function BufferReadUInt16BE (offset, noAssert) { 801 | return _readUInt16(this, offset, false, noAssert) 802 | } 803 | 804 | function _readUInt32 (buf, offset, littleEndian, noAssert) { 805 | if (!noAssert) { 806 | assert(typeof (littleEndian) === 'boolean', 807 | 'missing or invalid endian') 808 | assert(offset !== undefined && offset !== null, 'missing offset') 809 | assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') 810 | } 811 | 812 | var len = buf.length 813 | if (offset >= len) { 814 | return 815 | } else if (offset + 3 >= len) { 816 | var dv = new xDataView(new xArrayBuffer(4)) 817 | for (var i = 0; i + offset < len; i++) { 818 | dv.setUint8(i, buf[i + offset]) 819 | } 820 | return dv.getUint32(0, littleEndian) 821 | } else { 822 | return buf._dataview.getUint32(offset, littleEndian) 823 | } 824 | } 825 | 826 | function BufferReadUInt32LE (offset, noAssert) { 827 | return _readUInt32(this, offset, true, noAssert) 828 | } 829 | 830 | function BufferReadUInt32BE (offset, noAssert) { 831 | return _readUInt32(this, offset, false, noAssert) 832 | } 833 | 834 | function BufferReadInt8 (offset, noAssert) { 835 | var buf = this 836 | if (!noAssert) { 837 | assert(offset !== undefined && offset !== null, 838 | 'missing offset') 839 | assert(offset < buf.length, 'Trying to read beyond buffer length') 840 | } 841 | 842 | if (offset >= buf.length) 843 | return 844 | 845 | return buf._dataview.getInt8(offset) 846 | } 847 | 848 | function _readInt16 (buf, offset, littleEndian, noAssert) { 849 | if (!noAssert) { 850 | assert(typeof (littleEndian) === 'boolean', 851 | 'missing or invalid endian') 852 | assert(offset !== undefined && offset !== null, 853 | 'missing offset') 854 | assert(offset + 1 < buf.length, 'Trying to read beyond buffer length') 855 | } 856 | 857 | var len = buf.length 858 | if (offset >= len) { 859 | return 860 | } else if (offset + 1 === len) { 861 | var dv = new xDataView(new xArrayBuffer(2)) 862 | dv.setUint8(0, buf[len - 1]) 863 | return dv.getInt16(0, littleEndian) 864 | } else { 865 | return buf._dataview.getInt16(offset, littleEndian) 866 | } 867 | } 868 | 869 | function BufferReadInt16LE (offset, noAssert) { 870 | return _readInt16(this, offset, true, noAssert) 871 | } 872 | 873 | function BufferReadInt16BE (offset, noAssert) { 874 | return _readInt16(this, offset, false, noAssert) 875 | } 876 | 877 | function _readInt32 (buf, offset, littleEndian, noAssert) { 878 | if (!noAssert) { 879 | assert(typeof (littleEndian) === 'boolean', 880 | 'missing or invalid endian') 881 | assert(offset !== undefined && offset !== null, 'missing offset') 882 | assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') 883 | } 884 | 885 | var len = buf.length 886 | if (offset >= len) { 887 | return 888 | } else if (offset + 3 >= len) { 889 | var dv = new xDataView(new xArrayBuffer(4)) 890 | for (var i = 0; i + offset < len; i++) { 891 | dv.setUint8(i, buf[i + offset]) 892 | } 893 | return dv.getInt32(0, littleEndian) 894 | } else { 895 | return buf._dataview.getInt32(offset, littleEndian) 896 | } 897 | } 898 | 899 | function BufferReadInt32LE (offset, noAssert) { 900 | return _readInt32(this, offset, true, noAssert) 901 | } 902 | 903 | function BufferReadInt32BE (offset, noAssert) { 904 | return _readInt32(this, offset, false, noAssert) 905 | } 906 | 907 | function _readFloat (buf, offset, littleEndian, noAssert) { 908 | if (!noAssert) { 909 | assert(typeof (littleEndian) === 'boolean', 910 | 'missing or invalid endian') 911 | assert(offset + 3 < buf.length, 'Trying to read beyond buffer length') 912 | } 913 | 914 | return buf._dataview.getFloat32(offset, littleEndian) 915 | } 916 | 917 | function BufferReadFloatLE (offset, noAssert) { 918 | return _readFloat(this, offset, true, noAssert) 919 | } 920 | 921 | function BufferReadFloatBE (offset, noAssert) { 922 | return _readFloat(this, offset, false, noAssert) 923 | } 924 | 925 | function _readDouble (buf, offset, littleEndian, noAssert) { 926 | if (!noAssert) { 927 | assert(typeof (littleEndian) === 'boolean', 928 | 'missing or invalid endian') 929 | assert(offset + 7 < buf.length, 'Trying to read beyond buffer length') 930 | } 931 | 932 | return buf._dataview.getFloat64(offset, littleEndian) 933 | } 934 | 935 | function BufferReadDoubleLE (offset, noAssert) { 936 | return _readDouble(this, offset, true, noAssert) 937 | } 938 | 939 | function BufferReadDoubleBE (offset, noAssert) { 940 | return _readDouble(this, offset, false, noAssert) 941 | } 942 | 943 | function BufferWriteUInt8 (value, offset, noAssert) { 944 | var buf = this 945 | if (!noAssert) { 946 | assert(value !== undefined && value !== null, 'missing value') 947 | assert(offset !== undefined && offset !== null, 'missing offset') 948 | assert(offset < buf.length, 'trying to write beyond buffer length') 949 | verifuint(value, 0xff) 950 | } 951 | 952 | if (offset >= buf.length) return 953 | 954 | buf[offset] = value 955 | } 956 | 957 | function _writeUInt16 (buf, value, offset, littleEndian, noAssert) { 958 | if (!noAssert) { 959 | assert(value !== undefined && value !== null, 'missing value') 960 | assert(typeof (littleEndian) === 'boolean', 961 | 'missing or invalid endian') 962 | assert(offset !== undefined && offset !== null, 'missing offset') 963 | assert(offset + 1 < buf.length, 'trying to write beyond buffer length') 964 | verifuint(value, 0xffff) 965 | } 966 | 967 | var len = buf.length 968 | if (offset >= len) { 969 | return 970 | } else if (offset + 1 === len) { 971 | var dv = new xDataView(new xArrayBuffer(2)) 972 | dv.setUint16(0, value, littleEndian) 973 | buf[offset] = dv.getUint8(0) 974 | } else { 975 | buf._dataview.setUint16(offset, value, littleEndian) 976 | } 977 | } 978 | 979 | function BufferWriteUInt16LE (value, offset, noAssert) { 980 | _writeUInt16(this, value, offset, true, noAssert) 981 | } 982 | 983 | function BufferWriteUInt16BE (value, offset, noAssert) { 984 | _writeUInt16(this, value, offset, false, noAssert) 985 | } 986 | 987 | function _writeUInt32 (buf, value, offset, littleEndian, noAssert) { 988 | if (!noAssert) { 989 | assert(value !== undefined && value !== null, 'missing value') 990 | assert(typeof (littleEndian) === 'boolean', 991 | 'missing or invalid endian') 992 | assert(offset !== undefined && offset !== null, 'missing offset') 993 | assert(offset + 3 < buf.length, 'trying to write beyond buffer length') 994 | verifuint(value, 0xffffffff) 995 | } 996 | 997 | var len = buf.length 998 | if (offset >= len) { 999 | return 1000 | } else if (offset + 3 >= len) { 1001 | var dv = new xDataView(new xArrayBuffer(4)) 1002 | dv.setUint32(0, value, littleEndian) 1003 | for (var i = 0; i + offset < len; i++) { 1004 | buf[i + offset] = dv.getUint8(i) 1005 | } 1006 | } else { 1007 | buf._dataview.setUint32(offset, value, littleEndian) 1008 | } 1009 | } 1010 | 1011 | function BufferWriteUInt32LE (value, offset, noAssert) { 1012 | _writeUInt32(this, value, offset, true, noAssert) 1013 | } 1014 | 1015 | function BufferWriteUInt32BE (value, offset, noAssert) { 1016 | _writeUInt32(this, value, offset, false, noAssert) 1017 | } 1018 | 1019 | function BufferWriteInt8 (value, offset, noAssert) { 1020 | var buf = this 1021 | if (!noAssert) { 1022 | assert(value !== undefined && value !== null, 'missing value') 1023 | assert(offset !== undefined && offset !== null, 'missing offset') 1024 | assert(offset < buf.length, 'Trying to write beyond buffer length') 1025 | verifsint(value, 0x7f, -0x80) 1026 | } 1027 | 1028 | if (offset >= buf.length) return 1029 | 1030 | buf._dataview.setInt8(offset, value) 1031 | } 1032 | 1033 | function _writeInt16 (buf, value, offset, littleEndian, noAssert) { 1034 | if (!noAssert) { 1035 | assert(value !== undefined && value !== null, 'missing value') 1036 | assert(typeof (littleEndian) === 'boolean', 1037 | 'missing or invalid endian') 1038 | assert(offset !== undefined && offset !== null, 'missing offset') 1039 | assert(offset + 1 < buf.length, 'Trying to write beyond buffer length') 1040 | verifsint(value, 0x7fff, -0x8000) 1041 | } 1042 | 1043 | var len = buf.length 1044 | if (offset >= len) { 1045 | return 1046 | } else if (offset + 1 === len) { 1047 | var dv = new xDataView(new xArrayBuffer(2)) 1048 | dv.setInt16(0, value, littleEndian) 1049 | buf[offset] = dv.getUint8(0) 1050 | } else { 1051 | buf._dataview.setInt16(offset, value, littleEndian) 1052 | } 1053 | } 1054 | 1055 | function BufferWriteInt16LE (value, offset, noAssert) { 1056 | _writeInt16(this, value, offset, true, noAssert) 1057 | } 1058 | 1059 | function BufferWriteInt16BE (value, offset, noAssert) { 1060 | _writeInt16(this, value, offset, false, noAssert) 1061 | } 1062 | 1063 | function _writeInt32 (buf, value, offset, littleEndian, noAssert) { 1064 | if (!noAssert) { 1065 | assert(value !== undefined && value !== null, 'missing value') 1066 | assert(typeof (littleEndian) === 'boolean', 1067 | 'missing or invalid endian') 1068 | assert(offset !== undefined && offset !== null, 'missing offset') 1069 | assert(offset + 3 < buf.length, 'Trying to write beyond buffer length') 1070 | verifsint(value, 0x7fffffff, -0x80000000) 1071 | } 1072 | 1073 | var len = buf.length 1074 | if (offset >= len) { 1075 | return 1076 | } else if (offset + 3 >= len) { 1077 | var dv = new xDataView(new xArrayBuffer(4)) 1078 | dv.setInt32(0, value, littleEndian) 1079 | for (var i = 0; i + offset < len; i++) { 1080 | buf[i + offset] = dv.getUint8(i) 1081 | } 1082 | } else { 1083 | buf._dataview.setInt32(offset, value, littleEndian) 1084 | } 1085 | } 1086 | 1087 | function BufferWriteInt32LE (value, offset, noAssert) { 1088 | _writeInt32(this, value, offset, true, noAssert) 1089 | } 1090 | 1091 | function BufferWriteInt32BE (value, offset, noAssert) { 1092 | _writeInt32(this, value, offset, false, noAssert) 1093 | } 1094 | 1095 | function _writeFloat (buf, value, offset, littleEndian, noAssert) { 1096 | if (!noAssert) { 1097 | assert(value !== undefined && value !== null, 'missing value') 1098 | assert(typeof (littleEndian) === 'boolean', 1099 | 'missing or invalid endian') 1100 | assert(offset !== undefined && offset !== null, 'missing offset') 1101 | assert(offset + 3 < buf.length, 'Trying to write beyond buffer length') 1102 | verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38) 1103 | } 1104 | 1105 | var len = buf.length 1106 | if (offset >= len) { 1107 | return 1108 | } else if (offset + 3 >= len) { 1109 | var dv = new xDataView(new xArrayBuffer(4)) 1110 | dv.setFloat32(0, value, littleEndian) 1111 | for (var i = 0; i + offset < len; i++) { 1112 | buf[i + offset] = dv.getUint8(i) 1113 | } 1114 | } else { 1115 | buf._dataview.setFloat32(offset, value, littleEndian) 1116 | } 1117 | } 1118 | 1119 | function BufferWriteFloatLE (value, offset, noAssert) { 1120 | _writeFloat(this, value, offset, true, noAssert) 1121 | } 1122 | 1123 | function BufferWriteFloatBE (value, offset, noAssert) { 1124 | _writeFloat(this, value, offset, false, noAssert) 1125 | } 1126 | 1127 | function _writeDouble (buf, value, offset, littleEndian, noAssert) { 1128 | if (!noAssert) { 1129 | assert(value !== undefined && value !== null, 'missing value') 1130 | assert(typeof (littleEndian) === 'boolean', 1131 | 'missing or invalid endian') 1132 | assert(offset !== undefined && offset !== null, 'missing offset') 1133 | assert(offset + 7 < buf.length, 1134 | 'Trying to write beyond buffer length') 1135 | verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308) 1136 | } 1137 | 1138 | var len = buf.length 1139 | if (offset >= len) { 1140 | return 1141 | } else if (offset + 7 >= len) { 1142 | var dv = new xDataView(new xArrayBuffer(8)) 1143 | dv.setFloat64(0, value, littleEndian) 1144 | for (var i = 0; i + offset < len; i++) { 1145 | buf[i + offset] = dv.getUint8(i) 1146 | } 1147 | } else { 1148 | buf._dataview.setFloat64(offset, value, littleEndian) 1149 | } 1150 | } 1151 | 1152 | function BufferWriteDoubleLE (value, offset, noAssert) { 1153 | _writeDouble(this, value, offset, true, noAssert) 1154 | } 1155 | 1156 | function BufferWriteDoubleBE (value, offset, noAssert) { 1157 | _writeDouble(this, value, offset, false, noAssert) 1158 | } 1159 | 1160 | // fill(value, start=0, end=buffer.length) 1161 | function BufferFill (value, start, end) { 1162 | if (!value) value = 0 1163 | if (!start) start = 0 1164 | if (!end) end = this.length 1165 | 1166 | if (typeof value === 'string') { 1167 | value = value.charCodeAt(0) 1168 | } 1169 | 1170 | if (typeof value !== 'number' || isNaN(value)) { 1171 | throw new Error('value is not a number') 1172 | } 1173 | 1174 | if (end < start) throw new Error('end < start') 1175 | 1176 | // Fill 0 bytes; we're done 1177 | if (end === start) return 1178 | if (this.length === 0) return 1179 | 1180 | if (start < 0 || start >= this.length) { 1181 | throw new Error('start out of bounds') 1182 | } 1183 | 1184 | if (end < 0 || end > this.length) { 1185 | throw new Error('end out of bounds') 1186 | } 1187 | 1188 | for (var i = start; i < end; i++) { 1189 | this[i] = value 1190 | } 1191 | } 1192 | 1193 | function BufferInspect () { 1194 | var out = [] 1195 | var len = this.length 1196 | for (var i = 0; i < len; i++) { 1197 | out[i] = toHex(this[i]) 1198 | if (i === exports.INSPECT_MAX_BYTES) { 1199 | out[i + 1] = '...' 1200 | break 1201 | } 1202 | } 1203 | return '' 1204 | } 1205 | 1206 | // Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance. 1207 | // Added in Node 0.12. 1208 | function BufferToArrayBuffer () { 1209 | return (new Buffer(this)).buffer 1210 | } 1211 | 1212 | 1213 | // HELPER FUNCTIONS 1214 | // ================ 1215 | 1216 | function stringtrim (str) { 1217 | if (str.trim) return str.trim() 1218 | return str.replace(/^\s+|\s+$/g, '') 1219 | } 1220 | 1221 | /** 1222 | * Check to see if the browser supports augmenting a `Uint8Array` instance. 1223 | * @return {boolean} 1224 | */ 1225 | function _browserSupport () { 1226 | var arr = new xUint8Array(0) 1227 | arr.foo = function () { return 42 } 1228 | 1229 | try { 1230 | return (42 === arr.foo()) 1231 | } catch (e) { 1232 | return false 1233 | } 1234 | } 1235 | 1236 | /** 1237 | * Class: ProxyBuffer 1238 | * ================== 1239 | * 1240 | * Only used in Firefox, since Firefox does not allow augmenting "native" 1241 | * objects (like Uint8Array instances) with new properties for some unknown 1242 | * (probably silly) reason. So we'll use an ES6 Proxy (supported since 1243 | * Firefox 18) to wrap the Uint8Array instance without actually adding any 1244 | * properties to it. 1245 | * 1246 | * Instances of this "fake" Buffer class are the "target" of the 1247 | * ES6 Proxy (see `augment` function). 1248 | * 1249 | * We couldn't just use the `Uint8Array` as the target of the `Proxy` because 1250 | * Proxies have an important limitation on trapping the `toString` method. 1251 | * `Object.prototype.toString.call(proxy)` gets called whenever something is 1252 | * implicitly cast to a String. Unfortunately, with a `Proxy` this 1253 | * unconditionally returns `Object.prototype.toString.call(target)` which would 1254 | * always return "[object Uint8Array]" if we used the `Uint8Array` instance as 1255 | * the target. And, remember, in Firefox we cannot redefine the `Uint8Array` 1256 | * instance's `toString` method. 1257 | * 1258 | * So, we use this `ProxyBuffer` class as the proxy's "target". Since this class 1259 | * has its own custom `toString` method, it will get called whenever `toString` 1260 | * gets called, implicitly or explicitly, on the `Proxy` instance. 1261 | * 1262 | * We also have to define the Uint8Array methods `subarray` and `set` on 1263 | * `ProxyBuffer` because if we didn't then `proxy.subarray(0)` would have its 1264 | * `this` set to `proxy` (a `Proxy` instance) which throws an exception in 1265 | * Firefox which expects it to be a `TypedArray` instance. 1266 | */ 1267 | function ProxyBuffer (arr) { 1268 | this._arr = arr 1269 | 1270 | if (arr.byteLength !== 0) 1271 | this._dataview = new xDataView(arr.buffer, arr.byteOffset, arr.byteLength) 1272 | } 1273 | 1274 | ProxyBuffer.prototype.write = BufferWrite 1275 | ProxyBuffer.prototype.toString = BufferToString 1276 | ProxyBuffer.prototype.toLocaleString = BufferToString 1277 | ProxyBuffer.prototype.toJSON = BufferToJSON 1278 | ProxyBuffer.prototype.copy = BufferCopy 1279 | ProxyBuffer.prototype.slice = BufferSlice 1280 | ProxyBuffer.prototype.readUInt8 = BufferReadUInt8 1281 | ProxyBuffer.prototype.readUInt16LE = BufferReadUInt16LE 1282 | ProxyBuffer.prototype.readUInt16BE = BufferReadUInt16BE 1283 | ProxyBuffer.prototype.readUInt32LE = BufferReadUInt32LE 1284 | ProxyBuffer.prototype.readUInt32BE = BufferReadUInt32BE 1285 | ProxyBuffer.prototype.readInt8 = BufferReadInt8 1286 | ProxyBuffer.prototype.readInt16LE = BufferReadInt16LE 1287 | ProxyBuffer.prototype.readInt16BE = BufferReadInt16BE 1288 | ProxyBuffer.prototype.readInt32LE = BufferReadInt32LE 1289 | ProxyBuffer.prototype.readInt32BE = BufferReadInt32BE 1290 | ProxyBuffer.prototype.readFloatLE = BufferReadFloatLE 1291 | ProxyBuffer.prototype.readFloatBE = BufferReadFloatBE 1292 | ProxyBuffer.prototype.readDoubleLE = BufferReadDoubleLE 1293 | ProxyBuffer.prototype.readDoubleBE = BufferReadDoubleBE 1294 | ProxyBuffer.prototype.writeUInt8 = BufferWriteUInt8 1295 | ProxyBuffer.prototype.writeUInt16LE = BufferWriteUInt16LE 1296 | ProxyBuffer.prototype.writeUInt16BE = BufferWriteUInt16BE 1297 | ProxyBuffer.prototype.writeUInt32LE = BufferWriteUInt32LE 1298 | ProxyBuffer.prototype.writeUInt32BE = BufferWriteUInt32BE 1299 | ProxyBuffer.prototype.writeInt8 = BufferWriteInt8 1300 | ProxyBuffer.prototype.writeInt16LE = BufferWriteInt16LE 1301 | ProxyBuffer.prototype.writeInt16BE = BufferWriteInt16BE 1302 | ProxyBuffer.prototype.writeInt32LE = BufferWriteInt32LE 1303 | ProxyBuffer.prototype.writeInt32BE = BufferWriteInt32BE 1304 | ProxyBuffer.prototype.writeFloatLE = BufferWriteFloatLE 1305 | ProxyBuffer.prototype.writeFloatBE = BufferWriteFloatBE 1306 | ProxyBuffer.prototype.writeDoubleLE = BufferWriteDoubleLE 1307 | ProxyBuffer.prototype.writeDoubleBE = BufferWriteDoubleBE 1308 | ProxyBuffer.prototype.fill = BufferFill 1309 | ProxyBuffer.prototype.inspect = BufferInspect 1310 | ProxyBuffer.prototype.toArrayBuffer = BufferToArrayBuffer 1311 | ProxyBuffer.prototype._isBuffer = true 1312 | ProxyBuffer.prototype.subarray = function () { 1313 | return this._arr.subarray.apply(this._arr, arguments) 1314 | } 1315 | ProxyBuffer.prototype.set = function () { 1316 | return this._arr.set.apply(this._arr, arguments) 1317 | } 1318 | 1319 | var ProxyHandler = { 1320 | get: function (target, name) { 1321 | if (name in target) return target[name] 1322 | else return target._arr[name] 1323 | }, 1324 | set: function (target, name, value) { 1325 | target._arr[name] = value 1326 | } 1327 | } 1328 | 1329 | function augment (arr) { 1330 | if (browserSupport === undefined) { 1331 | browserSupport = _browserSupport() 1332 | } 1333 | 1334 | if (browserSupport) { 1335 | // Augment the Uint8Array *instance* (not the class!) with Buffer methods 1336 | arr.write = BufferWrite 1337 | arr.toString = BufferToString 1338 | arr.toLocaleString = BufferToString 1339 | arr.toJSON = BufferToJSON 1340 | arr.copy = BufferCopy 1341 | arr.slice = BufferSlice 1342 | arr.readUInt8 = BufferReadUInt8 1343 | arr.readUInt16LE = BufferReadUInt16LE 1344 | arr.readUInt16BE = BufferReadUInt16BE 1345 | arr.readUInt32LE = BufferReadUInt32LE 1346 | arr.readUInt32BE = BufferReadUInt32BE 1347 | arr.readInt8 = BufferReadInt8 1348 | arr.readInt16LE = BufferReadInt16LE 1349 | arr.readInt16BE = BufferReadInt16BE 1350 | arr.readInt32LE = BufferReadInt32LE 1351 | arr.readInt32BE = BufferReadInt32BE 1352 | arr.readFloatLE = BufferReadFloatLE 1353 | arr.readFloatBE = BufferReadFloatBE 1354 | arr.readDoubleLE = BufferReadDoubleLE 1355 | arr.readDoubleBE = BufferReadDoubleBE 1356 | arr.writeUInt8 = BufferWriteUInt8 1357 | arr.writeUInt16LE = BufferWriteUInt16LE 1358 | arr.writeUInt16BE = BufferWriteUInt16BE 1359 | arr.writeUInt32LE = BufferWriteUInt32LE 1360 | arr.writeUInt32BE = BufferWriteUInt32BE 1361 | arr.writeInt8 = BufferWriteInt8 1362 | arr.writeInt16LE = BufferWriteInt16LE 1363 | arr.writeInt16BE = BufferWriteInt16BE 1364 | arr.writeInt32LE = BufferWriteInt32LE 1365 | arr.writeInt32BE = BufferWriteInt32BE 1366 | arr.writeFloatLE = BufferWriteFloatLE 1367 | arr.writeFloatBE = BufferWriteFloatBE 1368 | arr.writeDoubleLE = BufferWriteDoubleLE 1369 | arr.writeDoubleBE = BufferWriteDoubleBE 1370 | arr.fill = BufferFill 1371 | arr.inspect = BufferInspect 1372 | arr.toArrayBuffer = BufferToArrayBuffer 1373 | arr._isBuffer = true 1374 | 1375 | if (arr.byteLength !== 0) 1376 | arr._dataview = new xDataView(arr.buffer, arr.byteOffset, arr.byteLength) 1377 | 1378 | return arr 1379 | 1380 | } else { 1381 | // This is a browser that doesn't support augmenting the `Uint8Array` 1382 | // instance (*ahem* Firefox) so use an ES6 `Proxy`. 1383 | var proxyBuffer = new ProxyBuffer(arr) 1384 | var proxy = new Proxy(proxyBuffer, ProxyHandler) 1385 | proxyBuffer._proxy = proxy 1386 | return proxy 1387 | } 1388 | } 1389 | 1390 | // slice(start, end) 1391 | function clamp (index, len, defaultValue) { 1392 | if (typeof index !== 'number') return defaultValue 1393 | index = ~~index; // Coerce to integer. 1394 | if (index >= len) return len 1395 | if (index >= 0) return index 1396 | index += len 1397 | if (index >= 0) return index 1398 | return 0 1399 | } 1400 | 1401 | function coerce (length) { 1402 | // Coerce length to a number (possibly NaN), round up 1403 | // in case it's fractional (e.g. 123.456) then do a 1404 | // double negate to coerce a NaN to 0. Easy, right? 1405 | length = ~~Math.ceil(+length) 1406 | return length < 0 ? 0 : length 1407 | } 1408 | 1409 | function isArrayIsh (subject) { 1410 | return Array.isArray(subject) || Buffer.isBuffer(subject) || 1411 | subject && typeof subject === 'object' && 1412 | typeof subject.length === 'number' 1413 | } 1414 | 1415 | function toHex (n) { 1416 | if (n < 16) return '0' + n.toString(16) 1417 | return n.toString(16) 1418 | } 1419 | 1420 | function utf8ToBytes (str) { 1421 | var byteArray = [] 1422 | for (var i = 0; i < str.length; i++) 1423 | if (str.charCodeAt(i) <= 0x7F) 1424 | byteArray.push(str.charCodeAt(i)) 1425 | else { 1426 | var h = encodeURIComponent(str.charAt(i)).substr(1).split('%') 1427 | for (var j = 0; j < h.length; j++) 1428 | byteArray.push(parseInt(h[j], 16)) 1429 | } 1430 | 1431 | return byteArray 1432 | } 1433 | 1434 | function asciiToBytes (str) { 1435 | var byteArray = [] 1436 | for (var i = 0; i < str.length; i++) { 1437 | // Node's code seems to be doing this and not & 0x7F.. 1438 | byteArray.push(str.charCodeAt(i) & 0xFF) 1439 | } 1440 | 1441 | return byteArray 1442 | } 1443 | 1444 | function base64ToBytes (str) { 1445 | return require('base64-js').toByteArray(str) 1446 | } 1447 | 1448 | function blitBuffer (src, dst, offset, length) { 1449 | var pos, i = 0 1450 | while (i < length) { 1451 | if ((i + offset >= dst.length) || (i >= src.length)) 1452 | break 1453 | 1454 | dst[i + offset] = src[i] 1455 | i++ 1456 | } 1457 | return i 1458 | } 1459 | 1460 | function decodeUtf8Char (str) { 1461 | try { 1462 | return decodeURIComponent(str) 1463 | } catch (err) { 1464 | return String.fromCharCode(0xFFFD) // UTF 8 invalid char 1465 | } 1466 | } 1467 | 1468 | /* 1469 | * We have to make sure that the value is a valid integer. This means that it 1470 | * is non-negative. It has no fractional component and that it does not 1471 | * exceed the maximum allowed value. 1472 | * 1473 | * value The number to check for validity 1474 | * 1475 | * max The maximum value 1476 | */ 1477 | function verifuint (value, max) { 1478 | assert(typeof (value) == 'number', 'cannot write a non-number as a number') 1479 | assert(value >= 0, 1480 | 'specified a negative value for writing an unsigned value') 1481 | assert(value <= max, 'value is larger than maximum value for type') 1482 | assert(Math.floor(value) === value, 'value has a fractional component') 1483 | } 1484 | 1485 | /* 1486 | * A series of checks to make sure we actually have a signed 32-bit number 1487 | */ 1488 | function verifsint(value, max, min) { 1489 | assert(typeof (value) == 'number', 'cannot write a non-number as a number') 1490 | assert(value <= max, 'value larger than maximum allowed value') 1491 | assert(value >= min, 'value smaller than minimum allowed value') 1492 | assert(Math.floor(value) === value, 'value has a fractional component') 1493 | } 1494 | 1495 | function verifIEEE754(value, max, min) { 1496 | assert(typeof (value) == 'number', 'cannot write a non-number as a number') 1497 | assert(value <= max, 'value larger than maximum allowed value') 1498 | assert(value >= min, 'value smaller than minimum allowed value') 1499 | } 1500 | 1501 | function assert (test, message) { 1502 | if (!test) throw new Error(message || 'Failed assertion') 1503 | } 1504 | 1505 | },{"base64-js":3,"typedarray":4}],"native-buffer-browserify":[function(require,module,exports){ 1506 | module.exports=require('PcZj9L'); 1507 | },{}],3:[function(require,module,exports){ 1508 | (function (exports) { 1509 | 'use strict'; 1510 | 1511 | var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; 1512 | 1513 | function b64ToByteArray(b64) { 1514 | var i, j, l, tmp, placeHolders, arr; 1515 | 1516 | if (b64.length % 4 > 0) { 1517 | throw 'Invalid string. Length must be a multiple of 4'; 1518 | } 1519 | 1520 | // the number of equal signs (place holders) 1521 | // if there are two placeholders, than the two characters before it 1522 | // represent one byte 1523 | // if there is only one, then the three characters before it represent 2 bytes 1524 | // this is just a cheap hack to not do indexOf twice 1525 | placeHolders = b64.indexOf('='); 1526 | placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0; 1527 | 1528 | // base64 is 4/3 + up to two characters of the original data 1529 | arr = [];//new Uint8Array(b64.length * 3 / 4 - placeHolders); 1530 | 1531 | // if there are placeholders, only get up to the last complete 4 chars 1532 | l = placeHolders > 0 ? b64.length - 4 : b64.length; 1533 | 1534 | for (i = 0, j = 0; i < l; i += 4, j += 3) { 1535 | tmp = (lookup.indexOf(b64[i]) << 18) | (lookup.indexOf(b64[i + 1]) << 12) | (lookup.indexOf(b64[i + 2]) << 6) | lookup.indexOf(b64[i + 3]); 1536 | arr.push((tmp & 0xFF0000) >> 16); 1537 | arr.push((tmp & 0xFF00) >> 8); 1538 | arr.push(tmp & 0xFF); 1539 | } 1540 | 1541 | if (placeHolders === 2) { 1542 | tmp = (lookup.indexOf(b64[i]) << 2) | (lookup.indexOf(b64[i + 1]) >> 4); 1543 | arr.push(tmp & 0xFF); 1544 | } else if (placeHolders === 1) { 1545 | tmp = (lookup.indexOf(b64[i]) << 10) | (lookup.indexOf(b64[i + 1]) << 4) | (lookup.indexOf(b64[i + 2]) >> 2); 1546 | arr.push((tmp >> 8) & 0xFF); 1547 | arr.push(tmp & 0xFF); 1548 | } 1549 | 1550 | return arr; 1551 | } 1552 | 1553 | function uint8ToBase64(uint8) { 1554 | var i, 1555 | extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes 1556 | output = "", 1557 | temp, length; 1558 | 1559 | function tripletToBase64 (num) { 1560 | return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]; 1561 | }; 1562 | 1563 | // go through the array every three bytes, we'll deal with trailing stuff later 1564 | for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) { 1565 | temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]); 1566 | output += tripletToBase64(temp); 1567 | } 1568 | 1569 | // pad the end with zeros, but make sure to not forget the extra bytes 1570 | switch (extraBytes) { 1571 | case 1: 1572 | temp = uint8[uint8.length - 1]; 1573 | output += lookup[temp >> 2]; 1574 | output += lookup[(temp << 4) & 0x3F]; 1575 | output += '=='; 1576 | break; 1577 | case 2: 1578 | temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]); 1579 | output += lookup[temp >> 10]; 1580 | output += lookup[(temp >> 4) & 0x3F]; 1581 | output += lookup[(temp << 2) & 0x3F]; 1582 | output += '='; 1583 | break; 1584 | } 1585 | 1586 | return output; 1587 | } 1588 | 1589 | module.exports.toByteArray = b64ToByteArray; 1590 | module.exports.fromByteArray = uint8ToBase64; 1591 | }()); 1592 | 1593 | },{}],4:[function(require,module,exports){ 1594 | var undefined = (void 0); // Paranoia 1595 | 1596 | // Beyond this value, index getters/setters (i.e. array[0], array[1]) are so slow to 1597 | // create, and consume so much memory, that the browser appears frozen. 1598 | var MAX_ARRAY_LENGTH = 1e5; 1599 | 1600 | // Approximations of internal ECMAScript conversion functions 1601 | var ECMAScript = (function() { 1602 | // Stash a copy in case other scripts modify these 1603 | var opts = Object.prototype.toString, 1604 | ophop = Object.prototype.hasOwnProperty; 1605 | 1606 | return { 1607 | // Class returns internal [[Class]] property, used to avoid cross-frame instanceof issues: 1608 | Class: function(v) { return opts.call(v).replace(/^\[object *|\]$/g, ''); }, 1609 | HasProperty: function(o, p) { return p in o; }, 1610 | HasOwnProperty: function(o, p) { return ophop.call(o, p); }, 1611 | IsCallable: function(o) { return typeof o === 'function'; }, 1612 | ToInt32: function(v) { return v >> 0; }, 1613 | ToUint32: function(v) { return v >>> 0; } 1614 | }; 1615 | }()); 1616 | 1617 | // Snapshot intrinsics 1618 | var LN2 = Math.LN2, 1619 | abs = Math.abs, 1620 | floor = Math.floor, 1621 | log = Math.log, 1622 | min = Math.min, 1623 | pow = Math.pow, 1624 | round = Math.round; 1625 | 1626 | // ES5: lock down object properties 1627 | function configureProperties(obj) { 1628 | if (getOwnPropertyNames && defineProperty) { 1629 | var props = getOwnPropertyNames(obj), i; 1630 | for (i = 0; i < props.length; i += 1) { 1631 | defineProperty(obj, props[i], { 1632 | value: obj[props[i]], 1633 | writable: false, 1634 | enumerable: false, 1635 | configurable: false 1636 | }); 1637 | } 1638 | } 1639 | } 1640 | 1641 | // emulate ES5 getter/setter API using legacy APIs 1642 | // http://blogs.msdn.com/b/ie/archive/2010/09/07/transitioning-existing-code-to-the-es5-getter-setter-apis.aspx 1643 | // (second clause tests for Object.defineProperty() in IE<9 that only supports extending DOM prototypes, but 1644 | // note that IE<9 does not support __defineGetter__ or __defineSetter__ so it just renders the method harmless) 1645 | var defineProperty = Object.defineProperty || function(o, p, desc) { 1646 | if (!o === Object(o)) throw new TypeError("Object.defineProperty called on non-object"); 1647 | if (ECMAScript.HasProperty(desc, 'get') && Object.prototype.__defineGetter__) { Object.prototype.__defineGetter__.call(o, p, desc.get); } 1648 | if (ECMAScript.HasProperty(desc, 'set') && Object.prototype.__defineSetter__) { Object.prototype.__defineSetter__.call(o, p, desc.set); } 1649 | if (ECMAScript.HasProperty(desc, 'value')) { o[p] = desc.value; } 1650 | return o; 1651 | }; 1652 | 1653 | var getOwnPropertyNames = Object.getOwnPropertyNames || function getOwnPropertyNames(o) { 1654 | if (o !== Object(o)) throw new TypeError("Object.getOwnPropertyNames called on non-object"); 1655 | var props = [], p; 1656 | for (p in o) { 1657 | if (ECMAScript.HasOwnProperty(o, p)) { 1658 | props.push(p); 1659 | } 1660 | } 1661 | return props; 1662 | }; 1663 | 1664 | // ES5: Make obj[index] an alias for obj._getter(index)/obj._setter(index, value) 1665 | // for index in 0 ... obj.length 1666 | function makeArrayAccessors(obj) { 1667 | if (!defineProperty) { return; } 1668 | 1669 | if (obj.length > MAX_ARRAY_LENGTH) throw new RangeError("Array too large for polyfill"); 1670 | 1671 | function makeArrayAccessor(index) { 1672 | defineProperty(obj, index, { 1673 | 'get': function() { return obj._getter(index); }, 1674 | 'set': function(v) { obj._setter(index, v); }, 1675 | enumerable: true, 1676 | configurable: false 1677 | }); 1678 | } 1679 | 1680 | var i; 1681 | for (i = 0; i < obj.length; i += 1) { 1682 | makeArrayAccessor(i); 1683 | } 1684 | } 1685 | 1686 | // Internal conversion functions: 1687 | // pack() - take a number (interpreted as Type), output a byte array 1688 | // unpack() - take a byte array, output a Type-like number 1689 | 1690 | function as_signed(value, bits) { var s = 32 - bits; return (value << s) >> s; } 1691 | function as_unsigned(value, bits) { var s = 32 - bits; return (value << s) >>> s; } 1692 | 1693 | function packI8(n) { return [n & 0xff]; } 1694 | function unpackI8(bytes) { return as_signed(bytes[0], 8); } 1695 | 1696 | function packU8(n) { return [n & 0xff]; } 1697 | function unpackU8(bytes) { return as_unsigned(bytes[0], 8); } 1698 | 1699 | function packU8Clamped(n) { n = round(Number(n)); return [n < 0 ? 0 : n > 0xff ? 0xff : n & 0xff]; } 1700 | 1701 | function packI16(n) { return [(n >> 8) & 0xff, n & 0xff]; } 1702 | function unpackI16(bytes) { return as_signed(bytes[0] << 8 | bytes[1], 16); } 1703 | 1704 | function packU16(n) { return [(n >> 8) & 0xff, n & 0xff]; } 1705 | function unpackU16(bytes) { return as_unsigned(bytes[0] << 8 | bytes[1], 16); } 1706 | 1707 | function packI32(n) { return [(n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff]; } 1708 | function unpackI32(bytes) { return as_signed(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3], 32); } 1709 | 1710 | function packU32(n) { return [(n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff]; } 1711 | function unpackU32(bytes) { return as_unsigned(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3], 32); } 1712 | 1713 | function packIEEE754(v, ebits, fbits) { 1714 | 1715 | var bias = (1 << (ebits - 1)) - 1, 1716 | s, e, f, ln, 1717 | i, bits, str, bytes; 1718 | 1719 | function roundToEven(n) { 1720 | var w = floor(n), f = n - w; 1721 | if (f < 0.5) 1722 | return w; 1723 | if (f > 0.5) 1724 | return w + 1; 1725 | return w % 2 ? w + 1 : w; 1726 | } 1727 | 1728 | // Compute sign, exponent, fraction 1729 | if (v !== v) { 1730 | // NaN 1731 | // http://dev.w3.org/2006/webapi/WebIDL/#es-type-mapping 1732 | e = (1 << ebits) - 1; f = pow(2, fbits - 1); s = 0; 1733 | } else if (v === Infinity || v === -Infinity) { 1734 | e = (1 << ebits) - 1; f = 0; s = (v < 0) ? 1 : 0; 1735 | } else if (v === 0) { 1736 | e = 0; f = 0; s = (1 / v === -Infinity) ? 1 : 0; 1737 | } else { 1738 | s = v < 0; 1739 | v = abs(v); 1740 | 1741 | if (v >= pow(2, 1 - bias)) { 1742 | e = min(floor(log(v) / LN2), 1023); 1743 | f = roundToEven(v / pow(2, e) * pow(2, fbits)); 1744 | if (f / pow(2, fbits) >= 2) { 1745 | e = e + 1; 1746 | f = 1; 1747 | } 1748 | if (e > bias) { 1749 | // Overflow 1750 | e = (1 << ebits) - 1; 1751 | f = 0; 1752 | } else { 1753 | // Normalized 1754 | e = e + bias; 1755 | f = f - pow(2, fbits); 1756 | } 1757 | } else { 1758 | // Denormalized 1759 | e = 0; 1760 | f = roundToEven(v / pow(2, 1 - bias - fbits)); 1761 | } 1762 | } 1763 | 1764 | // Pack sign, exponent, fraction 1765 | bits = []; 1766 | for (i = fbits; i; i -= 1) { bits.push(f % 2 ? 1 : 0); f = floor(f / 2); } 1767 | for (i = ebits; i; i -= 1) { bits.push(e % 2 ? 1 : 0); e = floor(e / 2); } 1768 | bits.push(s ? 1 : 0); 1769 | bits.reverse(); 1770 | str = bits.join(''); 1771 | 1772 | // Bits to bytes 1773 | bytes = []; 1774 | while (str.length) { 1775 | bytes.push(parseInt(str.substring(0, 8), 2)); 1776 | str = str.substring(8); 1777 | } 1778 | return bytes; 1779 | } 1780 | 1781 | function unpackIEEE754(bytes, ebits, fbits) { 1782 | 1783 | // Bytes to bits 1784 | var bits = [], i, j, b, str, 1785 | bias, s, e, f; 1786 | 1787 | for (i = bytes.length; i; i -= 1) { 1788 | b = bytes[i - 1]; 1789 | for (j = 8; j; j -= 1) { 1790 | bits.push(b % 2 ? 1 : 0); b = b >> 1; 1791 | } 1792 | } 1793 | bits.reverse(); 1794 | str = bits.join(''); 1795 | 1796 | // Unpack sign, exponent, fraction 1797 | bias = (1 << (ebits - 1)) - 1; 1798 | s = parseInt(str.substring(0, 1), 2) ? -1 : 1; 1799 | e = parseInt(str.substring(1, 1 + ebits), 2); 1800 | f = parseInt(str.substring(1 + ebits), 2); 1801 | 1802 | // Produce number 1803 | if (e === (1 << ebits) - 1) { 1804 | return f !== 0 ? NaN : s * Infinity; 1805 | } else if (e > 0) { 1806 | // Normalized 1807 | return s * pow(2, e - bias) * (1 + f / pow(2, fbits)); 1808 | } else if (f !== 0) { 1809 | // Denormalized 1810 | return s * pow(2, -(bias - 1)) * (f / pow(2, fbits)); 1811 | } else { 1812 | return s < 0 ? -0 : 0; 1813 | } 1814 | } 1815 | 1816 | function unpackF64(b) { return unpackIEEE754(b, 11, 52); } 1817 | function packF64(v) { return packIEEE754(v, 11, 52); } 1818 | function unpackF32(b) { return unpackIEEE754(b, 8, 23); } 1819 | function packF32(v) { return packIEEE754(v, 8, 23); } 1820 | 1821 | 1822 | // 1823 | // 3 The ArrayBuffer Type 1824 | // 1825 | 1826 | (function() { 1827 | 1828 | /** @constructor */ 1829 | var ArrayBuffer = function ArrayBuffer(length) { 1830 | length = ECMAScript.ToInt32(length); 1831 | if (length < 0) throw new RangeError('ArrayBuffer size is not a small enough positive integer'); 1832 | 1833 | this.byteLength = length; 1834 | this._bytes = []; 1835 | this._bytes.length = length; 1836 | 1837 | var i; 1838 | for (i = 0; i < this.byteLength; i += 1) { 1839 | this._bytes[i] = 0; 1840 | } 1841 | 1842 | configureProperties(this); 1843 | }; 1844 | 1845 | exports.ArrayBuffer = exports.ArrayBuffer || ArrayBuffer; 1846 | 1847 | // 1848 | // 4 The ArrayBufferView Type 1849 | // 1850 | 1851 | // NOTE: this constructor is not exported 1852 | /** @constructor */ 1853 | var ArrayBufferView = function ArrayBufferView() { 1854 | //this.buffer = null; 1855 | //this.byteOffset = 0; 1856 | //this.byteLength = 0; 1857 | }; 1858 | 1859 | // 1860 | // 5 The Typed Array View Types 1861 | // 1862 | 1863 | function makeConstructor(bytesPerElement, pack, unpack) { 1864 | // Each TypedArray type requires a distinct constructor instance with 1865 | // identical logic, which this produces. 1866 | 1867 | var ctor; 1868 | ctor = function(buffer, byteOffset, length) { 1869 | var array, sequence, i, s; 1870 | 1871 | if (!arguments.length || typeof arguments[0] === 'number') { 1872 | // Constructor(unsigned long length) 1873 | this.length = ECMAScript.ToInt32(arguments[0]); 1874 | if (length < 0) throw new RangeError('ArrayBufferView size is not a small enough positive integer'); 1875 | 1876 | this.byteLength = this.length * this.BYTES_PER_ELEMENT; 1877 | this.buffer = new ArrayBuffer(this.byteLength); 1878 | this.byteOffset = 0; 1879 | } else if (typeof arguments[0] === 'object' && arguments[0].constructor === ctor) { 1880 | // Constructor(TypedArray array) 1881 | array = arguments[0]; 1882 | 1883 | this.length = array.length; 1884 | this.byteLength = this.length * this.BYTES_PER_ELEMENT; 1885 | this.buffer = new ArrayBuffer(this.byteLength); 1886 | this.byteOffset = 0; 1887 | 1888 | for (i = 0; i < this.length; i += 1) { 1889 | this._setter(i, array._getter(i)); 1890 | } 1891 | } else if (typeof arguments[0] === 'object' && 1892 | !(arguments[0] instanceof ArrayBuffer || ECMAScript.Class(arguments[0]) === 'ArrayBuffer')) { 1893 | // Constructor(sequence array) 1894 | sequence = arguments[0]; 1895 | 1896 | this.length = ECMAScript.ToUint32(sequence.length); 1897 | this.byteLength = this.length * this.BYTES_PER_ELEMENT; 1898 | this.buffer = new ArrayBuffer(this.byteLength); 1899 | this.byteOffset = 0; 1900 | 1901 | for (i = 0; i < this.length; i += 1) { 1902 | s = sequence[i]; 1903 | this._setter(i, Number(s)); 1904 | } 1905 | } else if (typeof arguments[0] === 'object' && 1906 | (arguments[0] instanceof ArrayBuffer || ECMAScript.Class(arguments[0]) === 'ArrayBuffer')) { 1907 | // Constructor(ArrayBuffer buffer, 1908 | // optional unsigned long byteOffset, optional unsigned long length) 1909 | this.buffer = buffer; 1910 | 1911 | this.byteOffset = ECMAScript.ToUint32(byteOffset); 1912 | if (this.byteOffset > this.buffer.byteLength) { 1913 | throw new RangeError("byteOffset out of range"); 1914 | } 1915 | 1916 | if (this.byteOffset % this.BYTES_PER_ELEMENT) { 1917 | // The given byteOffset must be a multiple of the element 1918 | // size of the specific type, otherwise an exception is raised. 1919 | throw new RangeError("ArrayBuffer length minus the byteOffset is not a multiple of the element size."); 1920 | } 1921 | 1922 | if (arguments.length < 3) { 1923 | this.byteLength = this.buffer.byteLength - this.byteOffset; 1924 | 1925 | if (this.byteLength % this.BYTES_PER_ELEMENT) { 1926 | throw new RangeError("length of buffer minus byteOffset not a multiple of the element size"); 1927 | } 1928 | this.length = this.byteLength / this.BYTES_PER_ELEMENT; 1929 | } else { 1930 | this.length = ECMAScript.ToUint32(length); 1931 | this.byteLength = this.length * this.BYTES_PER_ELEMENT; 1932 | } 1933 | 1934 | if ((this.byteOffset + this.byteLength) > this.buffer.byteLength) { 1935 | throw new RangeError("byteOffset and length reference an area beyond the end of the buffer"); 1936 | } 1937 | } else { 1938 | throw new TypeError("Unexpected argument type(s)"); 1939 | } 1940 | 1941 | this.constructor = ctor; 1942 | 1943 | configureProperties(this); 1944 | makeArrayAccessors(this); 1945 | }; 1946 | 1947 | ctor.prototype = new ArrayBufferView(); 1948 | ctor.prototype.BYTES_PER_ELEMENT = bytesPerElement; 1949 | ctor.prototype._pack = pack; 1950 | ctor.prototype._unpack = unpack; 1951 | ctor.BYTES_PER_ELEMENT = bytesPerElement; 1952 | 1953 | // getter type (unsigned long index); 1954 | ctor.prototype._getter = function(index) { 1955 | if (arguments.length < 1) throw new SyntaxError("Not enough arguments"); 1956 | 1957 | index = ECMAScript.ToUint32(index); 1958 | if (index >= this.length) { 1959 | return undefined; 1960 | } 1961 | 1962 | var bytes = [], i, o; 1963 | for (i = 0, o = this.byteOffset + index * this.BYTES_PER_ELEMENT; 1964 | i < this.BYTES_PER_ELEMENT; 1965 | i += 1, o += 1) { 1966 | bytes.push(this.buffer._bytes[o]); 1967 | } 1968 | return this._unpack(bytes); 1969 | }; 1970 | 1971 | // NONSTANDARD: convenience alias for getter: type get(unsigned long index); 1972 | ctor.prototype.get = ctor.prototype._getter; 1973 | 1974 | // setter void (unsigned long index, type value); 1975 | ctor.prototype._setter = function(index, value) { 1976 | if (arguments.length < 2) throw new SyntaxError("Not enough arguments"); 1977 | 1978 | index = ECMAScript.ToUint32(index); 1979 | if (index >= this.length) { 1980 | return undefined; 1981 | } 1982 | 1983 | var bytes = this._pack(value), i, o; 1984 | for (i = 0, o = this.byteOffset + index * this.BYTES_PER_ELEMENT; 1985 | i < this.BYTES_PER_ELEMENT; 1986 | i += 1, o += 1) { 1987 | this.buffer._bytes[o] = bytes[i]; 1988 | } 1989 | }; 1990 | 1991 | // void set(TypedArray array, optional unsigned long offset); 1992 | // void set(sequence array, optional unsigned long offset); 1993 | ctor.prototype.set = function(index, value) { 1994 | if (arguments.length < 1) throw new SyntaxError("Not enough arguments"); 1995 | var array, sequence, offset, len, 1996 | i, s, d, 1997 | byteOffset, byteLength, tmp; 1998 | 1999 | if (typeof arguments[0] === 'object' && arguments[0].constructor === this.constructor) { 2000 | // void set(TypedArray array, optional unsigned long offset); 2001 | array = arguments[0]; 2002 | offset = ECMAScript.ToUint32(arguments[1]); 2003 | 2004 | if (offset + array.length > this.length) { 2005 | throw new RangeError("Offset plus length of array is out of range"); 2006 | } 2007 | 2008 | byteOffset = this.byteOffset + offset * this.BYTES_PER_ELEMENT; 2009 | byteLength = array.length * this.BYTES_PER_ELEMENT; 2010 | 2011 | if (array.buffer === this.buffer) { 2012 | tmp = []; 2013 | for (i = 0, s = array.byteOffset; i < byteLength; i += 1, s += 1) { 2014 | tmp[i] = array.buffer._bytes[s]; 2015 | } 2016 | for (i = 0, d = byteOffset; i < byteLength; i += 1, d += 1) { 2017 | this.buffer._bytes[d] = tmp[i]; 2018 | } 2019 | } else { 2020 | for (i = 0, s = array.byteOffset, d = byteOffset; 2021 | i < byteLength; i += 1, s += 1, d += 1) { 2022 | this.buffer._bytes[d] = array.buffer._bytes[s]; 2023 | } 2024 | } 2025 | } else if (typeof arguments[0] === 'object' && typeof arguments[0].length !== 'undefined') { 2026 | // void set(sequence array, optional unsigned long offset); 2027 | sequence = arguments[0]; 2028 | len = ECMAScript.ToUint32(sequence.length); 2029 | offset = ECMAScript.ToUint32(arguments[1]); 2030 | 2031 | if (offset + len > this.length) { 2032 | throw new RangeError("Offset plus length of array is out of range"); 2033 | } 2034 | 2035 | for (i = 0; i < len; i += 1) { 2036 | s = sequence[i]; 2037 | this._setter(offset + i, Number(s)); 2038 | } 2039 | } else { 2040 | throw new TypeError("Unexpected argument type(s)"); 2041 | } 2042 | }; 2043 | 2044 | // TypedArray subarray(long begin, optional long end); 2045 | ctor.prototype.subarray = function(start, end) { 2046 | function clamp(v, min, max) { return v < min ? min : v > max ? max : v; } 2047 | 2048 | start = ECMAScript.ToInt32(start); 2049 | end = ECMAScript.ToInt32(end); 2050 | 2051 | if (arguments.length < 1) { start = 0; } 2052 | if (arguments.length < 2) { end = this.length; } 2053 | 2054 | if (start < 0) { start = this.length + start; } 2055 | if (end < 0) { end = this.length + end; } 2056 | 2057 | start = clamp(start, 0, this.length); 2058 | end = clamp(end, 0, this.length); 2059 | 2060 | var len = end - start; 2061 | if (len < 0) { 2062 | len = 0; 2063 | } 2064 | 2065 | return new this.constructor( 2066 | this.buffer, this.byteOffset + start * this.BYTES_PER_ELEMENT, len); 2067 | }; 2068 | 2069 | return ctor; 2070 | } 2071 | 2072 | var Int8Array = makeConstructor(1, packI8, unpackI8); 2073 | var Uint8Array = makeConstructor(1, packU8, unpackU8); 2074 | var Uint8ClampedArray = makeConstructor(1, packU8Clamped, unpackU8); 2075 | var Int16Array = makeConstructor(2, packI16, unpackI16); 2076 | var Uint16Array = makeConstructor(2, packU16, unpackU16); 2077 | var Int32Array = makeConstructor(4, packI32, unpackI32); 2078 | var Uint32Array = makeConstructor(4, packU32, unpackU32); 2079 | var Float32Array = makeConstructor(4, packF32, unpackF32); 2080 | var Float64Array = makeConstructor(8, packF64, unpackF64); 2081 | 2082 | exports.Int8Array = exports.Int8Array || Int8Array; 2083 | exports.Uint8Array = exports.Uint8Array || Uint8Array; 2084 | exports.Uint8ClampedArray = exports.Uint8ClampedArray || Uint8ClampedArray; 2085 | exports.Int16Array = exports.Int16Array || Int16Array; 2086 | exports.Uint16Array = exports.Uint16Array || Uint16Array; 2087 | exports.Int32Array = exports.Int32Array || Int32Array; 2088 | exports.Uint32Array = exports.Uint32Array || Uint32Array; 2089 | exports.Float32Array = exports.Float32Array || Float32Array; 2090 | exports.Float64Array = exports.Float64Array || Float64Array; 2091 | }()); 2092 | 2093 | // 2094 | // 6 The DataView View Type 2095 | // 2096 | 2097 | (function() { 2098 | function r(array, index) { 2099 | return ECMAScript.IsCallable(array.get) ? array.get(index) : array[index]; 2100 | } 2101 | 2102 | var IS_BIG_ENDIAN = (function() { 2103 | var u16array = new(exports.Uint16Array)([0x1234]), 2104 | u8array = new(exports.Uint8Array)(u16array.buffer); 2105 | return r(u8array, 0) === 0x12; 2106 | }()); 2107 | 2108 | // Constructor(ArrayBuffer buffer, 2109 | // optional unsigned long byteOffset, 2110 | // optional unsigned long byteLength) 2111 | /** @constructor */ 2112 | var DataView = function DataView(buffer, byteOffset, byteLength) { 2113 | if (arguments.length === 0) { 2114 | buffer = new ArrayBuffer(0); 2115 | } else if (!(buffer instanceof ArrayBuffer || ECMAScript.Class(buffer) === 'ArrayBuffer')) { 2116 | throw new TypeError("TypeError"); 2117 | } 2118 | 2119 | this.buffer = buffer || new ArrayBuffer(0); 2120 | 2121 | this.byteOffset = ECMAScript.ToUint32(byteOffset); 2122 | if (this.byteOffset > this.buffer.byteLength) { 2123 | throw new RangeError("byteOffset out of range"); 2124 | } 2125 | 2126 | if (arguments.length < 3) { 2127 | this.byteLength = this.buffer.byteLength - this.byteOffset; 2128 | } else { 2129 | this.byteLength = ECMAScript.ToUint32(byteLength); 2130 | } 2131 | 2132 | if ((this.byteOffset + this.byteLength) > this.buffer.byteLength) { 2133 | throw new RangeError("byteOffset and length reference an area beyond the end of the buffer"); 2134 | } 2135 | 2136 | configureProperties(this); 2137 | }; 2138 | 2139 | function makeGetter(arrayType) { 2140 | return function(byteOffset, littleEndian) { 2141 | 2142 | byteOffset = ECMAScript.ToUint32(byteOffset); 2143 | 2144 | if (byteOffset + arrayType.BYTES_PER_ELEMENT > this.byteLength) { 2145 | throw new RangeError("Array index out of range"); 2146 | } 2147 | byteOffset += this.byteOffset; 2148 | 2149 | var uint8Array = new Uint8Array(this.buffer, byteOffset, arrayType.BYTES_PER_ELEMENT), 2150 | bytes = [], i; 2151 | for (i = 0; i < arrayType.BYTES_PER_ELEMENT; i += 1) { 2152 | bytes.push(r(uint8Array, i)); 2153 | } 2154 | 2155 | if (Boolean(littleEndian) === Boolean(IS_BIG_ENDIAN)) { 2156 | bytes.reverse(); 2157 | } 2158 | 2159 | return r(new arrayType(new Uint8Array(bytes).buffer), 0); 2160 | }; 2161 | } 2162 | 2163 | DataView.prototype.getUint8 = makeGetter(exports.Uint8Array); 2164 | DataView.prototype.getInt8 = makeGetter(exports.Int8Array); 2165 | DataView.prototype.getUint16 = makeGetter(exports.Uint16Array); 2166 | DataView.prototype.getInt16 = makeGetter(exports.Int16Array); 2167 | DataView.prototype.getUint32 = makeGetter(exports.Uint32Array); 2168 | DataView.prototype.getInt32 = makeGetter(exports.Int32Array); 2169 | DataView.prototype.getFloat32 = makeGetter(exports.Float32Array); 2170 | DataView.prototype.getFloat64 = makeGetter(exports.Float64Array); 2171 | 2172 | function makeSetter(arrayType) { 2173 | return function(byteOffset, value, littleEndian) { 2174 | 2175 | byteOffset = ECMAScript.ToUint32(byteOffset); 2176 | if (byteOffset + arrayType.BYTES_PER_ELEMENT > this.byteLength) { 2177 | throw new RangeError("Array index out of range"); 2178 | } 2179 | 2180 | // Get bytes 2181 | var typeArray = new arrayType([value]), 2182 | byteArray = new Uint8Array(typeArray.buffer), 2183 | bytes = [], i, byteView; 2184 | 2185 | for (i = 0; i < arrayType.BYTES_PER_ELEMENT; i += 1) { 2186 | bytes.push(r(byteArray, i)); 2187 | } 2188 | 2189 | // Flip if necessary 2190 | if (Boolean(littleEndian) === Boolean(IS_BIG_ENDIAN)) { 2191 | bytes.reverse(); 2192 | } 2193 | 2194 | // Write them 2195 | byteView = new Uint8Array(this.buffer, byteOffset, arrayType.BYTES_PER_ELEMENT); 2196 | byteView.set(bytes); 2197 | }; 2198 | } 2199 | 2200 | DataView.prototype.setUint8 = makeSetter(exports.Uint8Array); 2201 | DataView.prototype.setInt8 = makeSetter(exports.Int8Array); 2202 | DataView.prototype.setUint16 = makeSetter(exports.Uint16Array); 2203 | DataView.prototype.setInt16 = makeSetter(exports.Int16Array); 2204 | DataView.prototype.setUint32 = makeSetter(exports.Uint32Array); 2205 | DataView.prototype.setInt32 = makeSetter(exports.Int32Array); 2206 | DataView.prototype.setFloat32 = makeSetter(exports.Float32Array); 2207 | DataView.prototype.setFloat64 = makeSetter(exports.Float64Array); 2208 | 2209 | exports.DataView = exports.DataView || DataView; 2210 | 2211 | }()); 2212 | 2213 | },{}]},{},[]) 2214 | ;;module.exports=require("native-buffer-browserify").Buffer 2215 | 2216 | },{}],5:[function(require,module,exports){ 2217 | // shim for using process in browser 2218 | 2219 | var process = module.exports = {}; 2220 | 2221 | process.nextTick = (function () { 2222 | var canSetImmediate = typeof window !== 'undefined' 2223 | && window.setImmediate; 2224 | var canPost = typeof window !== 'undefined' 2225 | && window.postMessage && window.addEventListener 2226 | ; 2227 | 2228 | if (canSetImmediate) { 2229 | return function (f) { return window.setImmediate(f) }; 2230 | } 2231 | 2232 | if (canPost) { 2233 | var queue = []; 2234 | window.addEventListener('message', function (ev) { 2235 | var source = ev.source; 2236 | if ((source === window || source === null) && ev.data === 'process-tick') { 2237 | ev.stopPropagation(); 2238 | if (queue.length > 0) { 2239 | var fn = queue.shift(); 2240 | fn(); 2241 | } 2242 | } 2243 | }, true); 2244 | 2245 | return function nextTick(fn) { 2246 | queue.push(fn); 2247 | window.postMessage('process-tick', '*'); 2248 | }; 2249 | } 2250 | 2251 | return function nextTick(fn) { 2252 | setTimeout(fn, 0); 2253 | }; 2254 | })(); 2255 | 2256 | process.title = 'browser'; 2257 | process.browser = true; 2258 | process.env = {}; 2259 | process.argv = []; 2260 | 2261 | process.binding = function (name) { 2262 | throw new Error('process.binding is not supported'); 2263 | } 2264 | 2265 | // TODO(shtylman) 2266 | process.cwd = function () { return '/' }; 2267 | process.chdir = function (dir) { 2268 | throw new Error('process.chdir is not supported'); 2269 | }; 2270 | 2271 | },{}],6:[function(require,module,exports){ 2272 | module.exports = function isBuffer(arg) { 2273 | return arg && typeof arg === 'object' 2274 | && typeof arg.copy === 'function' 2275 | && typeof arg.fill === 'function' 2276 | && typeof arg.readUInt8 === 'function'; 2277 | } 2278 | },{}],7:[function(require,module,exports){ 2279 | var process=require("__browserify_process"),global=typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {};// Copyright Joyent, Inc. and other Node contributors. 2280 | // 2281 | // Permission is hereby granted, free of charge, to any person obtaining a 2282 | // copy of this software and associated documentation files (the 2283 | // "Software"), to deal in the Software without restriction, including 2284 | // without limitation the rights to use, copy, modify, merge, publish, 2285 | // distribute, sublicense, and/or sell copies of the Software, and to permit 2286 | // persons to whom the Software is furnished to do so, subject to the 2287 | // following conditions: 2288 | // 2289 | // The above copyright notice and this permission notice shall be included 2290 | // in all copies or substantial portions of the Software. 2291 | // 2292 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 2293 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2294 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 2295 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 2296 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 2297 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 2298 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 2299 | 2300 | var formatRegExp = /%[sdj%]/g; 2301 | exports.format = function(f) { 2302 | if (!isString(f)) { 2303 | var objects = []; 2304 | for (var i = 0; i < arguments.length; i++) { 2305 | objects.push(inspect(arguments[i])); 2306 | } 2307 | return objects.join(' '); 2308 | } 2309 | 2310 | var i = 1; 2311 | var args = arguments; 2312 | var len = args.length; 2313 | var str = String(f).replace(formatRegExp, function(x) { 2314 | if (x === '%%') return '%'; 2315 | if (i >= len) return x; 2316 | switch (x) { 2317 | case '%s': return String(args[i++]); 2318 | case '%d': return Number(args[i++]); 2319 | case '%j': 2320 | try { 2321 | return JSON.stringify(args[i++]); 2322 | } catch (_) { 2323 | return '[Circular]'; 2324 | } 2325 | default: 2326 | return x; 2327 | } 2328 | }); 2329 | for (var x = args[i]; i < len; x = args[++i]) { 2330 | if (isNull(x) || !isObject(x)) { 2331 | str += ' ' + x; 2332 | } else { 2333 | str += ' ' + inspect(x); 2334 | } 2335 | } 2336 | return str; 2337 | }; 2338 | 2339 | 2340 | // Mark that a method should not be used. 2341 | // Returns a modified function which warns once by default. 2342 | // If --no-deprecation is set, then it is a no-op. 2343 | exports.deprecate = function(fn, msg) { 2344 | // Allow for deprecating things in the process of starting up. 2345 | if (isUndefined(global.process)) { 2346 | return function() { 2347 | return exports.deprecate(fn, msg).apply(this, arguments); 2348 | }; 2349 | } 2350 | 2351 | if (process.noDeprecation === true) { 2352 | return fn; 2353 | } 2354 | 2355 | var warned = false; 2356 | function deprecated() { 2357 | if (!warned) { 2358 | if (process.throwDeprecation) { 2359 | throw new Error(msg); 2360 | } else if (process.traceDeprecation) { 2361 | console.trace(msg); 2362 | } else { 2363 | console.error(msg); 2364 | } 2365 | warned = true; 2366 | } 2367 | return fn.apply(this, arguments); 2368 | } 2369 | 2370 | return deprecated; 2371 | }; 2372 | 2373 | 2374 | var debugs = {}; 2375 | var debugEnviron; 2376 | exports.debuglog = function(set) { 2377 | if (isUndefined(debugEnviron)) 2378 | debugEnviron = process.env.NODE_DEBUG || ''; 2379 | set = set.toUpperCase(); 2380 | if (!debugs[set]) { 2381 | if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { 2382 | var pid = process.pid; 2383 | debugs[set] = function() { 2384 | var msg = exports.format.apply(exports, arguments); 2385 | console.error('%s %d: %s', set, pid, msg); 2386 | }; 2387 | } else { 2388 | debugs[set] = function() {}; 2389 | } 2390 | } 2391 | return debugs[set]; 2392 | }; 2393 | 2394 | 2395 | /** 2396 | * Echos the value of a value. Trys to print the value out 2397 | * in the best way possible given the different types. 2398 | * 2399 | * @param {Object} obj The object to print out. 2400 | * @param {Object} opts Optional options object that alters the output. 2401 | */ 2402 | /* legacy: obj, showHidden, depth, colors*/ 2403 | function inspect(obj, opts) { 2404 | // default options 2405 | var ctx = { 2406 | seen: [], 2407 | stylize: stylizeNoColor 2408 | }; 2409 | // legacy... 2410 | if (arguments.length >= 3) ctx.depth = arguments[2]; 2411 | if (arguments.length >= 4) ctx.colors = arguments[3]; 2412 | if (isBoolean(opts)) { 2413 | // legacy... 2414 | ctx.showHidden = opts; 2415 | } else if (opts) { 2416 | // got an "options" object 2417 | exports._extend(ctx, opts); 2418 | } 2419 | // set default options 2420 | if (isUndefined(ctx.showHidden)) ctx.showHidden = false; 2421 | if (isUndefined(ctx.depth)) ctx.depth = 2; 2422 | if (isUndefined(ctx.colors)) ctx.colors = false; 2423 | if (isUndefined(ctx.customInspect)) ctx.customInspect = true; 2424 | if (ctx.colors) ctx.stylize = stylizeWithColor; 2425 | return formatValue(ctx, obj, ctx.depth); 2426 | } 2427 | exports.inspect = inspect; 2428 | 2429 | 2430 | // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics 2431 | inspect.colors = { 2432 | 'bold' : [1, 22], 2433 | 'italic' : [3, 23], 2434 | 'underline' : [4, 24], 2435 | 'inverse' : [7, 27], 2436 | 'white' : [37, 39], 2437 | 'grey' : [90, 39], 2438 | 'black' : [30, 39], 2439 | 'blue' : [34, 39], 2440 | 'cyan' : [36, 39], 2441 | 'green' : [32, 39], 2442 | 'magenta' : [35, 39], 2443 | 'red' : [31, 39], 2444 | 'yellow' : [33, 39] 2445 | }; 2446 | 2447 | // Don't use 'blue' not visible on cmd.exe 2448 | inspect.styles = { 2449 | 'special': 'cyan', 2450 | 'number': 'yellow', 2451 | 'boolean': 'yellow', 2452 | 'undefined': 'grey', 2453 | 'null': 'bold', 2454 | 'string': 'green', 2455 | 'date': 'magenta', 2456 | // "name": intentionally not styling 2457 | 'regexp': 'red' 2458 | }; 2459 | 2460 | 2461 | function stylizeWithColor(str, styleType) { 2462 | var style = inspect.styles[styleType]; 2463 | 2464 | if (style) { 2465 | return '\u001b[' + inspect.colors[style][0] + 'm' + str + 2466 | '\u001b[' + inspect.colors[style][1] + 'm'; 2467 | } else { 2468 | return str; 2469 | } 2470 | } 2471 | 2472 | 2473 | function stylizeNoColor(str, styleType) { 2474 | return str; 2475 | } 2476 | 2477 | 2478 | function arrayToHash(array) { 2479 | var hash = {}; 2480 | 2481 | array.forEach(function(val, idx) { 2482 | hash[val] = true; 2483 | }); 2484 | 2485 | return hash; 2486 | } 2487 | 2488 | 2489 | function formatValue(ctx, value, recurseTimes) { 2490 | // Provide a hook for user-specified inspect functions. 2491 | // Check that value is an object with an inspect function on it 2492 | if (ctx.customInspect && 2493 | value && 2494 | isFunction(value.inspect) && 2495 | // Filter out the util module, it's inspect function is special 2496 | value.inspect !== exports.inspect && 2497 | // Also filter out any prototype objects using the circular check. 2498 | !(value.constructor && value.constructor.prototype === value)) { 2499 | var ret = value.inspect(recurseTimes, ctx); 2500 | if (!isString(ret)) { 2501 | ret = formatValue(ctx, ret, recurseTimes); 2502 | } 2503 | return ret; 2504 | } 2505 | 2506 | // Primitive types cannot have properties 2507 | var primitive = formatPrimitive(ctx, value); 2508 | if (primitive) { 2509 | return primitive; 2510 | } 2511 | 2512 | // Look up the keys of the object. 2513 | var keys = Object.keys(value); 2514 | var visibleKeys = arrayToHash(keys); 2515 | 2516 | if (ctx.showHidden) { 2517 | keys = Object.getOwnPropertyNames(value); 2518 | } 2519 | 2520 | // IE doesn't make error fields non-enumerable 2521 | // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx 2522 | if (isError(value) 2523 | && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) { 2524 | return formatError(value); 2525 | } 2526 | 2527 | // Some type of object without properties can be shortcutted. 2528 | if (keys.length === 0) { 2529 | if (isFunction(value)) { 2530 | var name = value.name ? ': ' + value.name : ''; 2531 | return ctx.stylize('[Function' + name + ']', 'special'); 2532 | } 2533 | if (isRegExp(value)) { 2534 | return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); 2535 | } 2536 | if (isDate(value)) { 2537 | return ctx.stylize(Date.prototype.toString.call(value), 'date'); 2538 | } 2539 | if (isError(value)) { 2540 | return formatError(value); 2541 | } 2542 | } 2543 | 2544 | var base = '', array = false, braces = ['{', '}']; 2545 | 2546 | // Make Array say that they are Array 2547 | if (isArray(value)) { 2548 | array = true; 2549 | braces = ['[', ']']; 2550 | } 2551 | 2552 | // Make functions say that they are functions 2553 | if (isFunction(value)) { 2554 | var n = value.name ? ': ' + value.name : ''; 2555 | base = ' [Function' + n + ']'; 2556 | } 2557 | 2558 | // Make RegExps say that they are RegExps 2559 | if (isRegExp(value)) { 2560 | base = ' ' + RegExp.prototype.toString.call(value); 2561 | } 2562 | 2563 | // Make dates with properties first say the date 2564 | if (isDate(value)) { 2565 | base = ' ' + Date.prototype.toUTCString.call(value); 2566 | } 2567 | 2568 | // Make error with message first say the error 2569 | if (isError(value)) { 2570 | base = ' ' + formatError(value); 2571 | } 2572 | 2573 | if (keys.length === 0 && (!array || value.length == 0)) { 2574 | return braces[0] + base + braces[1]; 2575 | } 2576 | 2577 | if (recurseTimes < 0) { 2578 | if (isRegExp(value)) { 2579 | return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); 2580 | } else { 2581 | return ctx.stylize('[Object]', 'special'); 2582 | } 2583 | } 2584 | 2585 | ctx.seen.push(value); 2586 | 2587 | var output; 2588 | if (array) { 2589 | output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); 2590 | } else { 2591 | output = keys.map(function(key) { 2592 | return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); 2593 | }); 2594 | } 2595 | 2596 | ctx.seen.pop(); 2597 | 2598 | return reduceToSingleString(output, base, braces); 2599 | } 2600 | 2601 | 2602 | function formatPrimitive(ctx, value) { 2603 | if (isUndefined(value)) 2604 | return ctx.stylize('undefined', 'undefined'); 2605 | if (isString(value)) { 2606 | var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') 2607 | .replace(/'/g, "\\'") 2608 | .replace(/\\"/g, '"') + '\''; 2609 | return ctx.stylize(simple, 'string'); 2610 | } 2611 | if (isNumber(value)) 2612 | return ctx.stylize('' + value, 'number'); 2613 | if (isBoolean(value)) 2614 | return ctx.stylize('' + value, 'boolean'); 2615 | // For some reason typeof null is "object", so special case here. 2616 | if (isNull(value)) 2617 | return ctx.stylize('null', 'null'); 2618 | } 2619 | 2620 | 2621 | function formatError(value) { 2622 | return '[' + Error.prototype.toString.call(value) + ']'; 2623 | } 2624 | 2625 | 2626 | function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { 2627 | var output = []; 2628 | for (var i = 0, l = value.length; i < l; ++i) { 2629 | if (hasOwnProperty(value, String(i))) { 2630 | output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, 2631 | String(i), true)); 2632 | } else { 2633 | output.push(''); 2634 | } 2635 | } 2636 | keys.forEach(function(key) { 2637 | if (!key.match(/^\d+$/)) { 2638 | output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, 2639 | key, true)); 2640 | } 2641 | }); 2642 | return output; 2643 | } 2644 | 2645 | 2646 | function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { 2647 | var name, str, desc; 2648 | desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] }; 2649 | if (desc.get) { 2650 | if (desc.set) { 2651 | str = ctx.stylize('[Getter/Setter]', 'special'); 2652 | } else { 2653 | str = ctx.stylize('[Getter]', 'special'); 2654 | } 2655 | } else { 2656 | if (desc.set) { 2657 | str = ctx.stylize('[Setter]', 'special'); 2658 | } 2659 | } 2660 | if (!hasOwnProperty(visibleKeys, key)) { 2661 | name = '[' + key + ']'; 2662 | } 2663 | if (!str) { 2664 | if (ctx.seen.indexOf(desc.value) < 0) { 2665 | if (isNull(recurseTimes)) { 2666 | str = formatValue(ctx, desc.value, null); 2667 | } else { 2668 | str = formatValue(ctx, desc.value, recurseTimes - 1); 2669 | } 2670 | if (str.indexOf('\n') > -1) { 2671 | if (array) { 2672 | str = str.split('\n').map(function(line) { 2673 | return ' ' + line; 2674 | }).join('\n').substr(2); 2675 | } else { 2676 | str = '\n' + str.split('\n').map(function(line) { 2677 | return ' ' + line; 2678 | }).join('\n'); 2679 | } 2680 | } 2681 | } else { 2682 | str = ctx.stylize('[Circular]', 'special'); 2683 | } 2684 | } 2685 | if (isUndefined(name)) { 2686 | if (array && key.match(/^\d+$/)) { 2687 | return str; 2688 | } 2689 | name = JSON.stringify('' + key); 2690 | if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { 2691 | name = name.substr(1, name.length - 2); 2692 | name = ctx.stylize(name, 'name'); 2693 | } else { 2694 | name = name.replace(/'/g, "\\'") 2695 | .replace(/\\"/g, '"') 2696 | .replace(/(^"|"$)/g, "'"); 2697 | name = ctx.stylize(name, 'string'); 2698 | } 2699 | } 2700 | 2701 | return name + ': ' + str; 2702 | } 2703 | 2704 | 2705 | function reduceToSingleString(output, base, braces) { 2706 | var numLinesEst = 0; 2707 | var length = output.reduce(function(prev, cur) { 2708 | numLinesEst++; 2709 | if (cur.indexOf('\n') >= 0) numLinesEst++; 2710 | return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; 2711 | }, 0); 2712 | 2713 | if (length > 60) { 2714 | return braces[0] + 2715 | (base === '' ? '' : base + '\n ') + 2716 | ' ' + 2717 | output.join(',\n ') + 2718 | ' ' + 2719 | braces[1]; 2720 | } 2721 | 2722 | return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; 2723 | } 2724 | 2725 | 2726 | // NOTE: These type checking functions intentionally don't use `instanceof` 2727 | // because it is fragile and can be easily faked with `Object.create()`. 2728 | function isArray(ar) { 2729 | return Array.isArray(ar); 2730 | } 2731 | exports.isArray = isArray; 2732 | 2733 | function isBoolean(arg) { 2734 | return typeof arg === 'boolean'; 2735 | } 2736 | exports.isBoolean = isBoolean; 2737 | 2738 | function isNull(arg) { 2739 | return arg === null; 2740 | } 2741 | exports.isNull = isNull; 2742 | 2743 | function isNullOrUndefined(arg) { 2744 | return arg == null; 2745 | } 2746 | exports.isNullOrUndefined = isNullOrUndefined; 2747 | 2748 | function isNumber(arg) { 2749 | return typeof arg === 'number'; 2750 | } 2751 | exports.isNumber = isNumber; 2752 | 2753 | function isString(arg) { 2754 | return typeof arg === 'string'; 2755 | } 2756 | exports.isString = isString; 2757 | 2758 | function isSymbol(arg) { 2759 | return typeof arg === 'symbol'; 2760 | } 2761 | exports.isSymbol = isSymbol; 2762 | 2763 | function isUndefined(arg) { 2764 | return arg === void 0; 2765 | } 2766 | exports.isUndefined = isUndefined; 2767 | 2768 | function isRegExp(re) { 2769 | return isObject(re) && objectToString(re) === '[object RegExp]'; 2770 | } 2771 | exports.isRegExp = isRegExp; 2772 | 2773 | function isObject(arg) { 2774 | return typeof arg === 'object' && arg !== null; 2775 | } 2776 | exports.isObject = isObject; 2777 | 2778 | function isDate(d) { 2779 | return isObject(d) && objectToString(d) === '[object Date]'; 2780 | } 2781 | exports.isDate = isDate; 2782 | 2783 | function isError(e) { 2784 | return isObject(e) && 2785 | (objectToString(e) === '[object Error]' || e instanceof Error); 2786 | } 2787 | exports.isError = isError; 2788 | 2789 | function isFunction(arg) { 2790 | return typeof arg === 'function'; 2791 | } 2792 | exports.isFunction = isFunction; 2793 | 2794 | function isPrimitive(arg) { 2795 | return arg === null || 2796 | typeof arg === 'boolean' || 2797 | typeof arg === 'number' || 2798 | typeof arg === 'string' || 2799 | typeof arg === 'symbol' || // ES6 symbol 2800 | typeof arg === 'undefined'; 2801 | } 2802 | exports.isPrimitive = isPrimitive; 2803 | 2804 | exports.isBuffer = require('./support/isBuffer'); 2805 | 2806 | function objectToString(o) { 2807 | return Object.prototype.toString.call(o); 2808 | } 2809 | 2810 | 2811 | function pad(n) { 2812 | return n < 10 ? '0' + n.toString(10) : n.toString(10); 2813 | } 2814 | 2815 | 2816 | var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 2817 | 'Oct', 'Nov', 'Dec']; 2818 | 2819 | // 26 Feb 16:19:34 2820 | function timestamp() { 2821 | var d = new Date(); 2822 | var time = [pad(d.getHours()), 2823 | pad(d.getMinutes()), 2824 | pad(d.getSeconds())].join(':'); 2825 | return [d.getDate(), months[d.getMonth()], time].join(' '); 2826 | } 2827 | 2828 | 2829 | // log is just a thin wrapper to console.log that prepends a timestamp 2830 | exports.log = function() { 2831 | console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments)); 2832 | }; 2833 | 2834 | 2835 | /** 2836 | * Inherit the prototype methods from one constructor into another. 2837 | * 2838 | * The Function.prototype.inherits from lang.js rewritten as a standalone 2839 | * function (not on Function.prototype). NOTE: If this file is to be loaded 2840 | * during bootstrapping this function needs to be rewritten using some native 2841 | * functions as prototype setup using normal JavaScript does not work as 2842 | * expected during bootstrapping (see mirror.js in r114903). 2843 | * 2844 | * @param {function} ctor Constructor function which needs to inherit the 2845 | * prototype. 2846 | * @param {function} superCtor Constructor function to inherit prototype from. 2847 | */ 2848 | exports.inherits = require('inherits'); 2849 | 2850 | exports._extend = function(origin, add) { 2851 | // Don't do anything if add isn't an object 2852 | if (!add || !isObject(add)) return origin; 2853 | 2854 | var keys = Object.keys(add); 2855 | var i = keys.length; 2856 | while (i--) { 2857 | origin[keys[i]] = add[keys[i]]; 2858 | } 2859 | return origin; 2860 | }; 2861 | 2862 | function hasOwnProperty(obj, prop) { 2863 | return Object.prototype.hasOwnProperty.call(obj, prop); 2864 | } 2865 | 2866 | },{"./support/isBuffer":6,"__browserify_process":5,"inherits":3}],8:[function(require,module,exports){ 2867 | // Taken from node's assert module, because it sucks 2868 | // and exposes next to nothing useful. 2869 | var util = require('./util'); 2870 | 2871 | module.exports = _deepEqual; 2872 | 2873 | var pSlice = Array.prototype.slice; 2874 | 2875 | function _deepEqual(actual, expected) { 2876 | // 7.1. All identical values are equivalent, as determined by ===. 2877 | if (actual === expected) { 2878 | return true; 2879 | 2880 | } else if (util.isBuffer(actual) && util.isBuffer(expected)) { 2881 | if (actual.length != expected.length) return false; 2882 | 2883 | for (var i = 0; i < actual.length; i++) { 2884 | if (actual[i] !== expected[i]) return false; 2885 | } 2886 | 2887 | return true; 2888 | 2889 | // 7.2. If the expected value is a Date object, the actual value is 2890 | // equivalent if it is also a Date object that refers to the same time. 2891 | } else if (util.isDate(actual) && util.isDate(expected)) { 2892 | return actual.getTime() === expected.getTime(); 2893 | 2894 | // 7.3 If the expected value is a RegExp object, the actual value is 2895 | // equivalent if it is also a RegExp object with the same source and 2896 | // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`). 2897 | } else if (util.isRegExp(actual) && util.isRegExp(expected)) { 2898 | return actual.source === expected.source && 2899 | actual.global === expected.global && 2900 | actual.multiline === expected.multiline && 2901 | actual.lastIndex === expected.lastIndex && 2902 | actual.ignoreCase === expected.ignoreCase; 2903 | 2904 | // 7.4. Other pairs that do not both pass typeof value == 'object', 2905 | // equivalence is determined by ==. 2906 | } else if (!util.isObject(actual) && !util.isObject(expected)) { 2907 | return actual == expected; 2908 | 2909 | // 7.5 For all other Object pairs, including Array objects, equivalence is 2910 | // determined by having the same number of owned properties (as verified 2911 | // with Object.prototype.hasOwnProperty.call), the same set of keys 2912 | // (although not necessarily the same order), equivalent values for every 2913 | // corresponding key, and an identical 'prototype' property. Note: this 2914 | // accounts for both named and indexed properties on Arrays. 2915 | } else { 2916 | return objEquiv(actual, expected); 2917 | } 2918 | } 2919 | 2920 | 2921 | function objEquiv (a, b) { 2922 | if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b)) 2923 | return false; 2924 | // an identical 'prototype' property. 2925 | if (a.prototype !== b.prototype) return false; 2926 | //~~~I've managed to break Object.keys through screwy arguments passing. 2927 | // Converting to array solves the problem. 2928 | if (util.isArguments(a)) { 2929 | if (!util.isArguments(b)) { 2930 | return false; 2931 | } 2932 | a = pSlice.call(a); 2933 | b = pSlice.call(b); 2934 | return _deepEqual(a, b); 2935 | } 2936 | try{ 2937 | var ka = Object.keys(a), 2938 | kb = Object.keys(b), 2939 | key, i; 2940 | } catch (e) {//happens when one is a string literal and the other isn't 2941 | return false; 2942 | } 2943 | // having the same number of owned properties (keys incorporates 2944 | // hasOwnProperty) 2945 | if (ka.length != kb.length) 2946 | return false; 2947 | //the same set of keys (although not necessarily the same order), 2948 | ka.sort(); 2949 | kb.sort(); 2950 | //~~~cheap key test 2951 | for (i = ka.length - 1; i >= 0; i--) { 2952 | if (ka[i] != kb[i]) 2953 | return false; 2954 | } 2955 | //equivalent values for every corresponding key, and 2956 | //~~~possibly expensive deep test 2957 | for (i = ka.length - 1; i >= 0; i--) { 2958 | key = ka[i]; 2959 | if (!_deepEqual(a[key], b[key])) return false; 2960 | } 2961 | return true; 2962 | } 2963 | 2964 | },{"./util":11}],9:[function(require,module,exports){ 2965 | //copy of node http module status codes 2966 | //https://github.com/joyent/node/blob/master/lib/_http_server.js 2967 | 2968 | var STATUS_CODES = exports.STATUS_CODES = { 2969 | 100 : 'Continue', 2970 | 101 : 'Switching Protocols', 2971 | 102 : 'Processing', // RFC 2518, obsoleted by RFC 4918 2972 | 200 : 'OK', 2973 | 201 : 'Created', 2974 | 202 : 'Accepted', 2975 | 203 : 'Non-Authoritative Information', 2976 | 204 : 'No Content', 2977 | 205 : 'Reset Content', 2978 | 206 : 'Partial Content', 2979 | 207 : 'Multi-Status', // RFC 4918 2980 | 300 : 'Multiple Choices', 2981 | 301 : 'Moved Permanently', 2982 | 302 : 'Moved Temporarily', 2983 | 303 : 'See Other', 2984 | 304 : 'Not Modified', 2985 | 305 : 'Use Proxy', 2986 | 307 : 'Temporary Redirect', 2987 | 400 : 'Bad Request', 2988 | 401 : 'Unauthorized', 2989 | 402 : 'Payment Required', 2990 | 403 : 'Forbidden', 2991 | 404 : 'Not Found', 2992 | 405 : 'Method Not Allowed', 2993 | 406 : 'Not Acceptable', 2994 | 407 : 'Proxy Authentication Required', 2995 | 408 : 'Request Time-out', 2996 | 409 : 'Conflict', 2997 | 410 : 'Gone', 2998 | 411 : 'Length Required', 2999 | 412 : 'Precondition Failed', 3000 | 413 : 'Request Entity Too Large', 3001 | 414 : 'Request-URI Too Large', 3002 | 415 : 'Unsupported Media Type', 3003 | 416 : 'Requested Range Not Satisfiable', 3004 | 417 : 'Expectation Failed', 3005 | 418 : 'I\'m a teapot', // RFC 2324 3006 | 422 : 'Unprocessable Entity', // RFC 4918 3007 | 423 : 'Locked', // RFC 4918 3008 | 424 : 'Failed Dependency', // RFC 4918 3009 | 425 : 'Unordered Collection', // RFC 4918 3010 | 426 : 'Upgrade Required', // RFC 2817 3011 | 428 : 'Precondition Required', // RFC 6585 3012 | 429 : 'Too Many Requests', // RFC 6585 3013 | 431 : 'Request Header Fields Too Large',// RFC 6585 3014 | 500 : 'Internal Server Error', 3015 | 501 : 'Not Implemented', 3016 | 502 : 'Bad Gateway', 3017 | 503 : 'Service Unavailable', 3018 | 504 : 'Gateway Time-out', 3019 | 505 : 'HTTP Version Not Supported', 3020 | 506 : 'Variant Also Negotiates', // RFC 2295 3021 | 507 : 'Insufficient Storage', // RFC 4918 3022 | 509 : 'Bandwidth Limit Exceeded', 3023 | 510 : 'Not Extended', // RFC 2774 3024 | 511 : 'Network Authentication Required' // RFC 6585 3025 | }; 3026 | 3027 | module.exports.STATUS_CODES = STATUS_CODES; 3028 | },{}],10:[function(require,module,exports){ 3029 | /*! 3030 | * Should 3031 | * Copyright(c) 2010-2012 TJ Holowaychuk 3032 | * MIT Licensed 3033 | */ 3034 | 3035 | /** 3036 | * Module dependencies. 3037 | */ 3038 | 3039 | var util = require('./util') 3040 | , assert = require('assert') 3041 | , AssertionError = assert.AssertionError 3042 | , statusCodes = require('./http').STATUS_CODES 3043 | , eql = require('./eql') 3044 | , inspect = require('util').inspect; 3045 | 3046 | /** 3047 | * Our function should 3048 | * @param obj 3049 | * @returns {Assertion} 3050 | */ 3051 | var should = function(obj) { 3052 | return new Assertion(util.isWrapperType(obj) ? obj.valueOf(): obj); 3053 | }; 3054 | 3055 | should.inspect = inspect; 3056 | 3057 | function i(value) { 3058 | if(util.isDate(value) && typeof value.inspect !== 'function') return value.toISOString(); //show millis in dates 3059 | return should.inspect(value); 3060 | } 3061 | 3062 | /** 3063 | * Expose assert to should 3064 | * 3065 | * This allows you to do things like below 3066 | * without require()ing the assert module. 3067 | * 3068 | * should.equal(foo.bar, undefined); 3069 | * 3070 | */ 3071 | util.merge(should, assert); 3072 | 3073 | 3074 | /** 3075 | * Assert _obj_ exists, with optional message. 3076 | * 3077 | * @param {*} obj 3078 | * @param {String} [msg] 3079 | * @api public 3080 | */ 3081 | should.exist = should.exists = function(obj, msg) { 3082 | if(null == obj) { 3083 | throw new AssertionError({ 3084 | message: msg || ('expected ' + i(obj) + ' to exist') 3085 | , stackStartFunction: should.exist 3086 | }); 3087 | } 3088 | }; 3089 | 3090 | /** 3091 | * Asserts _obj_ does not exist, with optional message. 3092 | * 3093 | * @param {*} obj 3094 | * @param {String} [msg] 3095 | * @api public 3096 | */ 3097 | 3098 | should.not = {}; 3099 | should.not.exist = should.not.exists = function(obj, msg){ 3100 | if (null != obj) { 3101 | throw new AssertionError({ 3102 | message: msg || ('expected ' + i(obj) + ' to not exist') 3103 | , stackStartFunction: should.not.exist 3104 | }); 3105 | } 3106 | }; 3107 | 3108 | /** 3109 | * Expose should to external world. 3110 | */ 3111 | exports = module.exports = should; 3112 | 3113 | 3114 | /** 3115 | * Expose api via `Object#should`. 3116 | * 3117 | * @api public 3118 | */ 3119 | 3120 | Object.defineProperty(Object.prototype, 'should', { 3121 | set: function(){}, 3122 | get: function(){ 3123 | return should(this); 3124 | }, 3125 | configurable: true 3126 | }); 3127 | 3128 | /** 3129 | * Initialize a new `Assertion` with the given _obj_. 3130 | * 3131 | * @param {*} obj 3132 | * @api private 3133 | */ 3134 | 3135 | var Assertion = should.Assertion = function Assertion(obj) { 3136 | this.obj = obj; 3137 | }; 3138 | 3139 | var hasOwnProperty = Object.prototype.hasOwnProperty; 3140 | 3141 | /** 3142 | * Prototype. 3143 | */ 3144 | 3145 | Assertion.prototype = { 3146 | 3147 | /** 3148 | * Assert _expr_ with the given _msg_ and _negatedMsg_. 3149 | * 3150 | * @param {Boolean} expr 3151 | * @param {function} msg 3152 | * @param {function} negatedMsg 3153 | * @param {Object} [expected] 3154 | * @param {Boolean} [showDiff] 3155 | * @param {String} [description] 3156 | * @api private 3157 | */ 3158 | 3159 | assert: function(expr, msg, negatedMsg, expected, showDiff, description){ 3160 | msg = this.negate ? negatedMsg : msg 3161 | 3162 | var ok = this.negate ? !expr : expr 3163 | , obj = this.obj; 3164 | 3165 | if (ok) return; 3166 | 3167 | var err = new AssertionError({ 3168 | message: msg.call(this) 3169 | , actual: obj 3170 | , expected: expected 3171 | , stackStartFunction: this.assert 3172 | , negated: this.negate 3173 | }); 3174 | 3175 | err.showDiff = showDiff; 3176 | err.description = description 3177 | 3178 | throw err; 3179 | }, 3180 | 3181 | /** 3182 | * Dummy getter. 3183 | * 3184 | * @api public 3185 | */ 3186 | 3187 | get an() { 3188 | return this; 3189 | }, 3190 | 3191 | /** 3192 | * Dummy getter. 3193 | * 3194 | * @api public 3195 | */ 3196 | 3197 | get of() { 3198 | return this; 3199 | }, 3200 | 3201 | /** 3202 | * Dummy getter. 3203 | * 3204 | * @api public 3205 | */ 3206 | 3207 | get a() { 3208 | return this; 3209 | }, 3210 | 3211 | /** 3212 | * Dummy getter. 3213 | * 3214 | * @api public 3215 | */ 3216 | 3217 | get and() { 3218 | return this; 3219 | }, 3220 | 3221 | /** 3222 | * Dummy getter. 3223 | * 3224 | * @api public 3225 | */ 3226 | 3227 | get be() { 3228 | return this; 3229 | }, 3230 | 3231 | /** 3232 | * Dummy getter. 3233 | * 3234 | * @api public 3235 | */ 3236 | 3237 | get have() { 3238 | return this; 3239 | }, 3240 | 3241 | /** 3242 | * Dummy getter. 3243 | * 3244 | * @api public 3245 | */ 3246 | 3247 | get with() { 3248 | return this; 3249 | }, 3250 | 3251 | /** 3252 | * Negation modifier. 3253 | * 3254 | * @api public 3255 | */ 3256 | 3257 | get not() { 3258 | this.negate = true; 3259 | return this; 3260 | }, 3261 | 3262 | /** 3263 | * Get object inspection string. 3264 | * 3265 | * @return {String} 3266 | * @api private 3267 | */ 3268 | 3269 | get inspect() { 3270 | return i(this.obj); 3271 | }, 3272 | 3273 | /** 3274 | * Assert instanceof `Arguments`. 3275 | * 3276 | * @api public 3277 | */ 3278 | 3279 | get arguments() { 3280 | this.assert( 3281 | util.isArguments(this.obj) 3282 | , function(){ return 'expected ' + this.inspect + ' to be arguments' } 3283 | , function(){ return 'expected ' + this.inspect + ' to not be arguments' }); 3284 | return this; 3285 | }, 3286 | 3287 | /** 3288 | * Assert that object is empty. 3289 | * 3290 | * @api public 3291 | */ 3292 | 3293 | get empty() { 3294 | var length = this.obj.length; 3295 | 3296 | if(util.isString(this.obj) || Array.isArray(this.obj) || util.isArguments(this.obj)) { 3297 | this.assert( 3298 | 0 === length 3299 | , function(){ return 'expected ' + this.inspect + ' to be empty' } 3300 | , function(){ return 'expected ' + this.inspect + ' not to be empty' }); 3301 | } else { 3302 | var ok = true; 3303 | for (var prop in this.obj) { 3304 | if(hasOwnProperty.call(this.obj, prop)) { 3305 | ok = false; 3306 | break; 3307 | } 3308 | } 3309 | 3310 | this.assert( 3311 | ok 3312 | , function(){ return 'expected ' + this.inspect + ' to be empty' } 3313 | , function(){ return 'expected ' + this.inspect + ' not to be empty' }); 3314 | 3315 | } 3316 | return this; 3317 | }, 3318 | 3319 | /** 3320 | * Assert ok. 3321 | * 3322 | * @api public 3323 | */ 3324 | 3325 | get ok() { 3326 | this.assert( 3327 | this.obj 3328 | , function(){ return 'expected ' + this.inspect + ' to be truthy' } 3329 | , function(){ return 'expected ' + this.inspect + ' to be falsey' }); 3330 | return this; 3331 | }, 3332 | 3333 | /** 3334 | * Assert true. 3335 | * 3336 | * @api public 3337 | */ 3338 | 3339 | get true() { 3340 | this.assert( 3341 | true === this.obj 3342 | , function(){ return 'expected ' + this.inspect + ' to be true' } 3343 | , function(){ return 'expected ' + this.inspect + ' not to be true' }); 3344 | return this; 3345 | }, 3346 | 3347 | /** 3348 | * Assert false. 3349 | * 3350 | * @api public 3351 | */ 3352 | 3353 | get false() { 3354 | this.assert( 3355 | false === this.obj 3356 | , function(){ return 'expected ' + this.inspect + ' to be false' } 3357 | , function(){ return 'expected ' + this.inspect + ' not to be false' }); 3358 | return this; 3359 | }, 3360 | 3361 | /** 3362 | * Assert NaN. 3363 | * 3364 | * @api public 3365 | */ 3366 | 3367 | get NaN() { 3368 | this.assert( 3369 | util.isNumber(this.obj) && isNaN(this.obj) 3370 | , function(){ return 'expected ' + this.inspect + ' to be NaN' } 3371 | , function(){ return 'expected ' + this.inspect + ' not to be NaN' }); 3372 | return this; 3373 | }, 3374 | 3375 | /** 3376 | * Assert Infinity. 3377 | * 3378 | * @api public 3379 | */ 3380 | 3381 | get Infinity() { 3382 | this.assert( 3383 | util.isNumber(this.obj) && !isNaN(this.obj) && !isFinite(this.obj) 3384 | , function(){ return 'expected ' + this.inspect + ' to be Infinity' } 3385 | , function(){ return 'expected ' + this.inspect + ' not to be Infinity' }); 3386 | return this; 3387 | }, 3388 | 3389 | /** 3390 | * Assert equal. 3391 | * 3392 | * @param {*} val 3393 | * @param {String} description 3394 | * @api public 3395 | */ 3396 | 3397 | eql: function(val, description){ 3398 | this.assert( 3399 | eql(val, this.obj) 3400 | , function(){ return 'expected ' + this.inspect + ' to equal ' + i(val) + (description ? " | " + description : "") } 3401 | , function(){ return 'expected ' + this.inspect + ' to not equal ' + i(val) + (description ? " | " + description : "") } 3402 | , val 3403 | , true 3404 | , description); 3405 | return this; 3406 | }, 3407 | 3408 | /** 3409 | * Assert strict equal. 3410 | * 3411 | * @param {*} val 3412 | * @param {String} description 3413 | * @api public 3414 | */ 3415 | 3416 | equal: function(val, description){ 3417 | this.assert( 3418 | val === this.obj 3419 | , function(){ return 'expected ' + this.inspect + ' to equal ' + i(val) + (description ? " | " + description : "") } 3420 | , function(){ return 'expected ' + this.inspect + ' to not equal ' + i(val) + (description ? " | " + description : "") } 3421 | , val 3422 | , void 0 3423 | , description); 3424 | return this; 3425 | }, 3426 | 3427 | /** 3428 | * Assert within start to finish (inclusive). 3429 | * 3430 | * @param {Number} start 3431 | * @param {Number} finish 3432 | * @param {String} description 3433 | * @api public 3434 | */ 3435 | 3436 | within: function(start, finish, description){ 3437 | var range = start + '..' + finish; 3438 | this.assert( 3439 | this.obj >= start && this.obj <= finish 3440 | , function(){ return 'expected ' + this.inspect + ' to be within ' + range + (description ? " | " + description : "") } 3441 | , function(){ return 'expected ' + this.inspect + ' to not be within ' + range + (description ? " | " + description : "") } 3442 | , void 0 3443 | , void 0 3444 | , description); 3445 | return this; 3446 | }, 3447 | 3448 | /** 3449 | * Assert within value +- delta (inclusive). 3450 | * 3451 | * @param {Number} value 3452 | * @param {Number} delta 3453 | * @param {String} description 3454 | * @api public 3455 | */ 3456 | 3457 | approximately: function(value, delta, description) { 3458 | this.assert( 3459 | Math.abs(this.obj - value) <= delta 3460 | , function(){ return 'expected ' + this.inspect + ' to be approximately ' + value + " +- " + delta + (description ? " | " + description : "") } 3461 | , function(){ return 'expected ' + this.inspect + ' to not be approximately ' + value + " +- " + delta + (description ? " | " + description : "") } 3462 | , void 0 3463 | , void 0 3464 | , description); 3465 | return this; 3466 | }, 3467 | 3468 | /** 3469 | * Assert typeof. 3470 | * 3471 | * @param {*} type 3472 | * @param {String} description 3473 | * @api public 3474 | */ 3475 | type: function(type, description){ 3476 | this.assert( 3477 | type == typeof this.obj 3478 | , function(){ return 'expected ' + this.inspect + ' to have type ' + type + (description ? " | " + description : "") } 3479 | , function(){ return 'expected ' + this.inspect + ' not to have type ' + type + (description ? " | " + description : "") } 3480 | , void 0 3481 | , void 0 3482 | , description); 3483 | return this; 3484 | }, 3485 | 3486 | /** 3487 | * Assert instanceof. 3488 | * 3489 | * @param {Function} constructor 3490 | * @param {String} description 3491 | * @api public 3492 | */ 3493 | 3494 | instanceof: function(constructor, description){ 3495 | var name = constructor.name; 3496 | this.assert( 3497 | this.obj instanceof constructor 3498 | , function(){ return 'expected ' + this.inspect + ' to be an instance of ' + name + (description ? " | " + description : "") } 3499 | , function(){ return 'expected ' + this.inspect + ' not to be an instance of ' + name + (description ? " | " + description : "") } 3500 | , void 0 3501 | , void 0 3502 | , description); 3503 | return this; 3504 | }, 3505 | 3506 | /** 3507 | * Assert if given object is a function. 3508 | */ 3509 | get Function(){ 3510 | this.assert( 3511 | util.isFunction(this.obj) 3512 | , function(){ return 'expected ' + this.inspect + ' to be a function' } 3513 | , function(){ return 'expected ' + this.inspect + ' not to be a function' }); 3514 | return this; 3515 | }, 3516 | 3517 | /** 3518 | * Assert given object is an object. 3519 | */ 3520 | get Object(){ 3521 | this.assert( 3522 | util.isObject(this.obj) && !Array.isArray(this.obj) 3523 | , function(){ return 'expected ' + this.inspect + ' to be an object' } 3524 | , function(){ return 'expected ' + this.inspect + ' not to be an object' }); 3525 | return this; 3526 | }, 3527 | 3528 | /** 3529 | * Assert given object is a string 3530 | */ 3531 | get String(){ 3532 | this.assert( 3533 | util.isString(this.obj) 3534 | , function(){ return 'expected ' + this.inspect + ' to be a string' } 3535 | , function(){ return 'expected ' + this.inspect + ' not to be a string' }); 3536 | return this; 3537 | }, 3538 | 3539 | /** 3540 | * Assert given object is an array 3541 | */ 3542 | get Array(){ 3543 | this.assert( 3544 | Array.isArray(this.obj) 3545 | , function(){ return 'expected ' + this.inspect + ' to be an array' } 3546 | , function(){ return 'expected ' + this.inspect + ' not to be an array' }); 3547 | return this; 3548 | }, 3549 | 3550 | /** 3551 | * Assert given object is a number. NaN and Infinity are not numbers. 3552 | */ 3553 | get Number(){ 3554 | this.assert( 3555 | util.isNumber(this.obj) && isFinite(this.obj) && !isNaN(this.obj) 3556 | , function(){ return 'expected ' + this.inspect + ' to be a number' } 3557 | , function(){ return 'expected ' + this.inspect + ' not to be a number' }); 3558 | return this; 3559 | }, 3560 | 3561 | /** 3562 | * Assert given object is a boolean 3563 | */ 3564 | get Boolean(){ 3565 | this.assert( 3566 | util.isBoolean(this.obj) 3567 | , function(){ return 'expected ' + this.inspect + ' to be a boolean' } 3568 | , function(){ return 'expected ' + this.inspect + ' not to be a boolean' }); 3569 | return this; 3570 | }, 3571 | 3572 | /** 3573 | * Assert given object is an error 3574 | */ 3575 | get Error() { 3576 | this.assert( 3577 | util.isError(this.obj) 3578 | , function(){ return 'expected ' + this.inspect + ' to be an error' } 3579 | , function(){ return 'expected ' + this.inspect + ' not to be an error' }); 3580 | return this; 3581 | }, 3582 | /** 3583 | * Assert numeric value above _n_. 3584 | * 3585 | * @param {Number} n 3586 | * @param {String} description 3587 | * @api public 3588 | */ 3589 | 3590 | above: function(n, description){ 3591 | this.assert( 3592 | this.obj > n 3593 | , function(){ return 'expected ' + this.inspect + ' to be above ' + n + (description ? " | " + description : "") } 3594 | , function(){ return 'expected ' + this.inspect + ' to be below ' + n + (description ? " | " + description : "") } 3595 | , void 0 3596 | , void 0 3597 | , description); 3598 | return this; 3599 | }, 3600 | 3601 | /** 3602 | * Assert numeric value below _n_. 3603 | * 3604 | * @param {Number} n 3605 | * @param {String} description 3606 | * @api public 3607 | */ 3608 | 3609 | below: function(n, description){ 3610 | this.assert( 3611 | this.obj < n 3612 | , function(){ return 'expected ' + this.inspect + ' to be below ' + n + (description ? " | " + description : "") } 3613 | , function(){ return 'expected ' + this.inspect + ' to be above ' + n + (description ? " | " + description : "") } 3614 | , void 0 3615 | , void 0 3616 | , description); 3617 | return this; 3618 | }, 3619 | 3620 | /** 3621 | * Assert string value matches _regexp_. 3622 | * 3623 | * @param {RegExp} regexp 3624 | * @param {String} description 3625 | * @api public 3626 | */ 3627 | 3628 | match: function(regexp, description){ 3629 | this.assert( 3630 | regexp.exec(this.obj) 3631 | , function(){ return 'expected ' + this.inspect + ' to match ' + regexp + (description ? " | " + description : "") } 3632 | , function(){ return 'expected ' + this.inspect + ' not to match ' + regexp + (description ? " | " + description : "") } 3633 | , void 0 3634 | , void 0 3635 | , description); 3636 | return this; 3637 | }, 3638 | 3639 | /** 3640 | * Assert property "length" exists and has value of _n_. 3641 | * 3642 | * @param {Number} n 3643 | * @param {String} description 3644 | * @api public 3645 | */ 3646 | 3647 | length: function(n, description){ 3648 | this.obj.should.have.property('length'); 3649 | var len = this.obj.length; 3650 | this.assert( 3651 | n == len 3652 | , function(){ return 'expected ' + this.inspect + ' to have a length of ' + n + ' but got ' + len + (description ? " | " + description : "") } 3653 | , function(){ return 'expected ' + this.inspect + ' to not have a length of ' + len + (description ? " | " + description : "") } 3654 | , void 0 3655 | , void 0 3656 | , description); 3657 | return this; 3658 | }, 3659 | 3660 | /** 3661 | * Assert property _name_ exists, with optional _val_. 3662 | * 3663 | * @param {String} name 3664 | * @param {*} [val] 3665 | * @param {String} description 3666 | * @api public 3667 | */ 3668 | 3669 | property: function(name, val, description){ 3670 | if (this.negate && undefined !== val) { 3671 | if (undefined === this.obj[name]) { 3672 | throw new Error(this.inspect + ' has no property ' + i(name) + (description ? " | " + description : "")); 3673 | } 3674 | } else { 3675 | this.assert( 3676 | undefined !== this.obj[name] 3677 | , function(){ return 'expected ' + this.inspect + ' to have a property ' + i(name) + (description ? " | " + description : "") } 3678 | , function(){ return 'expected ' + this.inspect + ' to not have a property ' + i(name) + (description ? " | " + description : "") } 3679 | , void 0 3680 | , void 0 3681 | , description); 3682 | } 3683 | 3684 | if (undefined !== val) { 3685 | this.assert( 3686 | val === this.obj[name] 3687 | , function(){ return 'expected ' + this.inspect + ' to have a property ' + i(name) 3688 | + ' of ' + i(val) + ', but got ' + i(this.obj[name]) + (description ? " | " + description : "") } 3689 | , function(){ return 'expected ' + this.inspect + ' to not have a property ' + i(name) + ' of ' + i(val) + (description ? " | " + description : "") } 3690 | , void 0 3691 | , void 0 3692 | , description); 3693 | } 3694 | 3695 | this.obj = this.obj[name]; 3696 | return this; 3697 | }, 3698 | /** 3699 | * Asset have given properties 3700 | * @param {Array|String ...} names 3701 | * @api public 3702 | */ 3703 | properties: function(names) { 3704 | var str 3705 | , ok = true; 3706 | 3707 | names = names instanceof Array 3708 | ? names 3709 | : Array.prototype.slice.call(arguments); 3710 | 3711 | var len = names.length; 3712 | 3713 | if (!len) throw new Error('names required'); 3714 | 3715 | // make sure they're all present 3716 | ok = names.every(function(name){ 3717 | return this.obj[name] !== undefined; 3718 | }, this); 3719 | 3720 | // key string 3721 | if (len > 1) { 3722 | names = names.map(function(name){ 3723 | return i(name); 3724 | }); 3725 | var last = names.pop(); 3726 | str = names.join(', ') + ', and ' + last; 3727 | } else { 3728 | str = i(names[0]); 3729 | } 3730 | 3731 | // message 3732 | str = 'have ' + (len > 1 ? 'properties ' : 'a property ') + str; 3733 | 3734 | this.assert( 3735 | ok 3736 | , function(){ return 'expected ' + this.inspect + ' to ' + str } 3737 | , function(){ return 'expected ' + this.inspect + ' to not ' + str }); 3738 | 3739 | return this; 3740 | }, 3741 | 3742 | /** 3743 | * Assert own property _name_ exists. 3744 | * 3745 | * @param {String} name 3746 | * @param {String} description 3747 | * @api public 3748 | */ 3749 | 3750 | ownProperty: function(name, description){ 3751 | this.assert( 3752 | hasOwnProperty.call(this.obj, name) 3753 | , function(){ return 'expected ' + this.inspect + ' to have own property ' + i(name) + (description ? " | " + description : "") } 3754 | , function(){ return 'expected ' + this.inspect + ' to not have own property ' + i(name) + (description ? " | " + description : "") } 3755 | , void 0 3756 | , void 0 3757 | , description); 3758 | this.obj = this.obj[name]; 3759 | return this; 3760 | }, 3761 | 3762 | /** 3763 | * Assert that string starts with `str`. 3764 | * @param {String} str 3765 | * @param {String} description 3766 | * @api public 3767 | */ 3768 | 3769 | startWith: function(str, description) { 3770 | this.assert(0 === this.obj.indexOf(str) 3771 | , function() { return 'expected ' + this.inspect + ' to start with ' + i(str) + (description ? " | " + description : "") } 3772 | , function() { return 'expected ' + this.inspect + ' to not start with ' + i(str) + (description ? " | " + description : "") } 3773 | , void 0 3774 | , void 0 3775 | , description); 3776 | return this; 3777 | }, 3778 | 3779 | /** 3780 | * Assert that string ends with `str`. 3781 | * @param {String} str 3782 | * @param {String} description 3783 | * @api public 3784 | */ 3785 | 3786 | endWith: function(str, description) { 3787 | this.assert(-1 !== this.obj.indexOf(str, this.obj.length - str.length) 3788 | , function() { return 'expected ' + this.inspect + ' to end with ' + i(str) + (description ? " | " + description : "") } 3789 | , function() { return 'expected ' + this.inspect + ' to not end with ' + i(str) + (description ? " | " + description : "") } 3790 | , void 0 3791 | , void 0 3792 | , description); 3793 | return this; 3794 | }, 3795 | 3796 | /** 3797 | * Assert that `obj` is present via `.indexOf()` or that `obj` contains some sub-object. 3798 | * 3799 | * @param {*} obj 3800 | * @param {String} description 3801 | * @api public 3802 | */ 3803 | 3804 | include: function(obj, description){ 3805 | if (!Array.isArray(this.obj) && !util.isString(this.obj)){ 3806 | var cmp = {}; 3807 | for (var key in obj) cmp[key] = this.obj[key]; 3808 | this.assert( 3809 | eql(cmp, obj) 3810 | , function(){ return 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (description ? " | " + description : "") } 3811 | , function(){ return 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (description ? " | " + description : "") } 3812 | , void 0 3813 | , void 0 3814 | , description); 3815 | } else { 3816 | this.assert( 3817 | ~this.obj.indexOf(obj) 3818 | , function(){ return 'expected ' + this.inspect + ' to include ' + i(obj) + (description ? " | " + description : "") } 3819 | , function(){ return 'expected ' + this.inspect + ' to not include ' + i(obj) + (description ? " | " + description : "") } 3820 | , void 0 3821 | , void 0 3822 | , description); 3823 | } 3824 | return this; 3825 | }, 3826 | 3827 | /** 3828 | * Assert that an object equal to `obj` is present. 3829 | * 3830 | * @param {Array} obj 3831 | * @param {String} description 3832 | * @api public 3833 | */ 3834 | 3835 | includeEql: function(obj, description){ 3836 | this.assert( 3837 | this.obj.some(function(item) { return eql(obj, item); }) 3838 | , function(){ return 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (description ? " | " + description : "") } 3839 | , function(){ return 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (description ? " | " + description : "") } 3840 | , void 0 3841 | , void 0 3842 | , description); 3843 | return this; 3844 | }, 3845 | 3846 | /** 3847 | * Assert exact keys or inclusion of keys by using 3848 | * the `.include` modifier. 3849 | * 3850 | * @param {Array|String ...} keys 3851 | * @api public 3852 | */ 3853 | 3854 | keys: function(keys){ 3855 | var str 3856 | , ok = true; 3857 | 3858 | keys = keys instanceof Array 3859 | ? keys 3860 | : Array.prototype.slice.call(arguments); 3861 | 3862 | if (!keys.length) throw new Error('keys required'); 3863 | 3864 | var actual = Object.keys(this.obj) 3865 | , len = keys.length; 3866 | 3867 | // make sure they're all present 3868 | ok = keys.every(function(key){ 3869 | return ~actual.indexOf(key); 3870 | }); 3871 | 3872 | // matching length 3873 | ok = ok && keys.length == actual.length; 3874 | 3875 | // key string 3876 | if (len > 1) { 3877 | keys = keys.map(function(key){ 3878 | return i(key); 3879 | }); 3880 | var last = keys.pop(); 3881 | str = keys.join(', ') + ', and ' + last; 3882 | } else { 3883 | str = i(keys[0]); 3884 | } 3885 | 3886 | // message 3887 | str = 'have ' + (len > 1 ? 'keys ' : 'key ') + str; 3888 | 3889 | this.assert( 3890 | ok 3891 | , function(){ return 'expected ' + this.inspect + ' to ' + str } 3892 | , function(){ return 'expected ' + this.inspect + ' to not ' + str }); 3893 | 3894 | return this; 3895 | }, 3896 | 3897 | /** 3898 | * Assert that header `field` has the given `val`. 3899 | * 3900 | * @param {String} field 3901 | * @param {String} val 3902 | * @return {Assertion} for chaining 3903 | * @api public 3904 | */ 3905 | 3906 | header: function(field, val){ 3907 | this.obj.should 3908 | .have.property('headers').and 3909 | .have.property(field.toLowerCase(), val); 3910 | return this; 3911 | }, 3912 | 3913 | /** 3914 | * Assert `.statusCode` of `code`. 3915 | * 3916 | * @param {Number} code 3917 | * @return {Assertion} for chaining 3918 | * @api public 3919 | */ 3920 | 3921 | status: function(code){ 3922 | this.obj.should.have.property('statusCode'); 3923 | var status = this.obj.statusCode; 3924 | 3925 | this.assert( 3926 | code == status 3927 | , function(){ return 'expected response code of ' + code + ' ' + i(statusCodes[code]) 3928 | + ', but got ' + status + ' ' + i(statusCodes[status]) } 3929 | , function(){ return 'expected to not respond with ' + code + ' ' + i(statusCodes[code]) }); 3930 | 3931 | return this; 3932 | }, 3933 | 3934 | /** 3935 | * Assert that this response has content-type: application/json. 3936 | * 3937 | * @return {Assertion} for chaining 3938 | * @api public 3939 | */ 3940 | 3941 | get json() { 3942 | this.obj.should.have.property('headers'); 3943 | this.obj.headers.should.have.property('content-type'); 3944 | this.obj.headers['content-type'].should.include('application/json'); 3945 | return this; 3946 | }, 3947 | 3948 | /** 3949 | * Assert that this response has content-type: text/html. 3950 | * 3951 | * @return {Assertion} for chaining 3952 | * @api public 3953 | */ 3954 | 3955 | get html() { 3956 | this.obj.should.have.property('headers'); 3957 | this.obj.headers.should.have.property('content-type'); 3958 | this.obj.headers['content-type'].should.include('text/html'); 3959 | return this; 3960 | }, 3961 | 3962 | /** 3963 | * Assert that this function will or will not 3964 | * throw an exception. 3965 | * 3966 | * @return {Assertion} for chaining 3967 | * @api public 3968 | */ 3969 | 3970 | throw: function(message){ 3971 | var fn = this.obj 3972 | , err = {} 3973 | , errorInfo = '' 3974 | , ok = true; 3975 | 3976 | try { 3977 | fn(); 3978 | ok = false; 3979 | } catch (e) { 3980 | err = e; 3981 | } 3982 | 3983 | if (ok) { 3984 | if ('string' == typeof message) { 3985 | ok = message == err.message; 3986 | } else if (message instanceof RegExp) { 3987 | ok = message.test(err.message); 3988 | } else if ('function' == typeof message) { 3989 | ok = err instanceof message; 3990 | } 3991 | 3992 | if (message && !ok) { 3993 | if ('string' == typeof message) { 3994 | errorInfo = " with a message matching '" + message + "', but got '" + err.message + "'"; 3995 | } else if (message instanceof RegExp) { 3996 | errorInfo = " with a message matching " + message + ", but got '" + err.message + "'"; 3997 | } else if ('function' == typeof message) { 3998 | errorInfo = " of type " + message.name + ", but got " + err.constructor.name; 3999 | } 4000 | } 4001 | } 4002 | 4003 | this.assert( 4004 | ok 4005 | , function(){ return 'expected an exception to be thrown' + errorInfo } 4006 | , function(){ return 'expected no exception to be thrown, got "' + err.message + '"' }); 4007 | 4008 | return this; 4009 | } 4010 | }; 4011 | 4012 | /** 4013 | * Aliases. 4014 | */ 4015 | 4016 | (function alias(name, as){ 4017 | Assertion.prototype[as] = Assertion.prototype[name]; 4018 | return alias; 4019 | }) 4020 | ('instanceof', 'instanceOf') 4021 | ('throw', 'throwError') 4022 | ('length', 'lengthOf') 4023 | ('keys', 'key') 4024 | ('ownProperty', 'haveOwnProperty') 4025 | ('above', 'greaterThan') 4026 | ('below', 'lessThan') 4027 | ('include', 'contain') 4028 | ('equal', 'exactly'); 4029 | 4030 | 4031 | },{"./eql":8,"./http":9,"./util":11,"assert":2,"util":7}],11:[function(require,module,exports){ 4032 | var Buffer=require("__browserify_Buffer");/** 4033 | * Check if given obj just a primitive type wrapper 4034 | * @param {Object} obj 4035 | * @returns {boolean} 4036 | * @api private 4037 | */ 4038 | exports.isWrapperType = function(obj) { 4039 | return isNumber(obj) || isString(obj) || isBoolean(obj); 4040 | } 4041 | 4042 | /** 4043 | * Merge object b with object a. 4044 | * 4045 | * var a = { foo: 'bar' } 4046 | * , b = { bar: 'baz' }; 4047 | * 4048 | * utils.merge(a, b); 4049 | * // => { foo: 'bar', bar: 'baz' } 4050 | * 4051 | * @param {Object} a 4052 | * @param {Object} b 4053 | * @return {Object} 4054 | * @api private 4055 | */ 4056 | 4057 | exports.merge = function(a, b){ 4058 | if (a && b) { 4059 | for (var key in b) { 4060 | a[key] = b[key]; 4061 | } 4062 | } 4063 | return a; 4064 | }; 4065 | 4066 | function isNumber(arg) { 4067 | return typeof arg === 'number' || arg instanceof Number; 4068 | } 4069 | 4070 | exports.isNumber = isNumber; 4071 | 4072 | function isString(arg) { 4073 | return typeof arg === 'string' || arg instanceof String; 4074 | } 4075 | 4076 | function isBoolean(arg) { 4077 | return typeof arg === 'boolean' || arg instanceof Boolean; 4078 | } 4079 | exports.isBoolean = isBoolean; 4080 | 4081 | exports.isString = isString; 4082 | 4083 | function isBuffer(arg) { 4084 | return typeof Buffer !== 'undefined' && arg instanceof Buffer; 4085 | } 4086 | 4087 | exports.isBuffer = isBuffer; 4088 | 4089 | function isDate(d) { 4090 | return isObject(d) && objectToString(d) === '[object Date]'; 4091 | } 4092 | 4093 | exports.isDate = isDate; 4094 | 4095 | function objectToString(o) { 4096 | return Object.prototype.toString.call(o); 4097 | } 4098 | 4099 | function isObject(arg) { 4100 | return typeof arg === 'object' && arg !== null; 4101 | } 4102 | 4103 | exports.isObject = isObject; 4104 | 4105 | function isRegExp(re) { 4106 | return isObject(re) && objectToString(re) === '[object RegExp]'; 4107 | } 4108 | 4109 | exports.isRegExp = isRegExp; 4110 | 4111 | function isNullOrUndefined(arg) { 4112 | return arg == null; 4113 | } 4114 | 4115 | exports.isNullOrUndefined = isNullOrUndefined; 4116 | 4117 | function isArguments(object) { 4118 | return objectToString(object) === '[object Arguments]'; 4119 | } 4120 | 4121 | exports.isArguments = isArguments; 4122 | 4123 | exports.isFunction = function(arg) { 4124 | return typeof arg === 'function' || arg instanceof Function; 4125 | }; 4126 | 4127 | function isError(e) { 4128 | return isObject(e) && objectToString(e) === '[object Error]'; 4129 | } 4130 | exports.isError = isError; 4131 | },{"__browserify_Buffer":4}]},{},[1]) -------------------------------------------------------------------------------- /examples/src/index.js: -------------------------------------------------------------------------------- 1 | var gulp = require("gulp"); -------------------------------------------------------------------------------- /examples/src/normal.js: -------------------------------------------------------------------------------- 1 | var should = require('should'); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var through = require('through2'); 2 | var gutil = require('gulp-util'); 3 | var PluginError = gutil.PluginError; 4 | var browserify = require('browserify'); 5 | var shim = require('browserify-shim'); 6 | var path = require('path'); 7 | var util = require('util'); 8 | var Readable = require('stream').Readable || require('readable-stream'); 9 | 10 | const PLUGIN_NAME = 'gulp-browserify'; 11 | 12 | function arrayStream(items) { 13 | var index = 0; 14 | var readable = new Readable({ objectMode: true }); 15 | readable._read = function() { 16 | if(index < items.length) { 17 | readable.push(items[index]); 18 | index++; 19 | } else { 20 | readable.push(null); 21 | } 22 | }; 23 | return readable; 24 | } 25 | 26 | function wrapWithPluginError(originalError){ 27 | var message, opts; 28 | 29 | if ('string' === typeof originalError) { 30 | message = originalError; 31 | } else { 32 | // Use annotated message of ParseError if available. 33 | // https://github.com/substack/node-syntax-error 34 | message = originalError.annotated || originalError.message; 35 | // Copy original properties that PluginError uses. 36 | opts = { 37 | name: originalError.name, 38 | stack: originalError.stack, 39 | fileName: originalError.fileName, 40 | lineNumber: originalError.lineNumber 41 | }; 42 | } 43 | 44 | return new PluginError(PLUGIN_NAME, message, opts); 45 | } 46 | 47 | module.exports = function(opts, data){ 48 | opts = opts || {}; 49 | data = data || {}; 50 | 51 | ['noParse', 'extensions', 'resolve'].forEach(function(opt){ 52 | if(opts[opt]) { 53 | data[opt] = opts[opt]; 54 | delete opts[opt]; 55 | } 56 | }); 57 | 58 | function transform(file, enc, cb){ 59 | var self = this; 60 | 61 | if (file.isStream()) { 62 | self.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported')); 63 | return cb(); 64 | } 65 | 66 | // browserify accepts file path or stream. 67 | 68 | if(file.isNull()) { 69 | data.entries = file.path; 70 | } 71 | 72 | if(file.isBuffer()) { 73 | data.entries = arrayStream([file.contents]); 74 | } 75 | 76 | data.basedir = path.dirname(file.path); 77 | 78 | // nobuiltins option 79 | if (!opts.builtins && opts.nobuiltins) { 80 | var nob = opts.nobuiltins; 81 | var builtins = require('./node_modules/browserify/lib/builtins.js'); 82 | nob = 'string' == typeof nob ? nob.split(' ') : nob; 83 | 84 | for (var i = 0; i < nob.length; i++) { 85 | delete builtins[nob[i]]; 86 | }; 87 | 88 | opts.builtins = builtins; 89 | } 90 | 91 | var bundler = browserify(data, opts); 92 | 93 | if(opts.shim) { 94 | for(var lib in opts.shim) { 95 | opts.shim[lib].path = path.resolve(opts.shim[lib].path); 96 | } 97 | bundler = shim(bundler, opts.shim); 98 | } 99 | 100 | bundler.on('error', function(err) { 101 | self.emit('error', wrapWithPluginError(err)); 102 | cb(); 103 | }); 104 | 105 | [ 106 | 'exclude', 107 | 'add', 108 | 'external', 109 | 'transform', 110 | 'ignore', 111 | 'require' 112 | ].forEach( function(method) { 113 | if (!opts[method]) return; 114 | [].concat(opts[method]).forEach(function (args) { 115 | bundler[method].apply(bundler, [].concat(args)); 116 | }); 117 | }); 118 | 119 | self.emit('prebundle', bundler); 120 | 121 | var bStream = bundler.bundle(opts, function(err, src){ 122 | if(err) { 123 | self.emit('error', wrapWithPluginError(err)); 124 | } else { 125 | self.emit('postbundle', src); 126 | 127 | file.contents = new Buffer(src); 128 | self.push(file); 129 | } 130 | 131 | cb(); 132 | }); 133 | } 134 | 135 | return through.obj(transform); 136 | }; 137 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-browserify", 3 | "version": "0.5.1", 4 | "description": "Bundle modules with Browserify", 5 | "license": "MIT", 6 | "repository": "https://github.com/deepak1556/gulp-browserify", 7 | "author": "Robo ", 8 | "contributors": [ 9 | "Robo ", 10 | "Steve Lacy ", 11 | "Shuhei Kagawa " 12 | ], 13 | "dependencies": { 14 | "browserify": "3.x", 15 | "gulp-util": "~2.2.5", 16 | "browserify-shim": "~2.0.10", 17 | "readable-stream": "~1.1.10", 18 | "through2": "~0.4.0" 19 | }, 20 | "main": "index.js", 21 | "engines": { 22 | "node": ">= 0.9" 23 | }, 24 | "scripts": { 25 | "test": "./node_modules/.bin/mocha" 26 | }, 27 | "devDependencies": { 28 | "mocha": "~1.17.1", 29 | "chai": "~1.9.0", 30 | "coffeeify": "~0.6.0" 31 | }, 32 | "keywords": [ 33 | "gulpplugin", 34 | "gulpfriendly", 35 | "browserify" 36 | ] 37 | } 38 | -------------------------------------------------------------------------------- /test/expected/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deepak1556/gulp-browserify/3481b6550c83d1bbbf980c5b423e21e91ceb3081/test/expected/.keep -------------------------------------------------------------------------------- /test/fixtures/add.js: -------------------------------------------------------------------------------- 1 | addValue = ADD_VALUE; 2 | -------------------------------------------------------------------------------- /test/fixtures/add_file.js: -------------------------------------------------------------------------------- 1 | ADD_VALUE = 100; 2 | -------------------------------------------------------------------------------- /test/fixtures/bar.js: -------------------------------------------------------------------------------- 1 | (function(window){ 2 | window.bar = 'foobar'; 3 | })(window); 4 | -------------------------------------------------------------------------------- /test/fixtures/custom_resolved.js: -------------------------------------------------------------------------------- 1 | module.exports = 'resolved content'; 2 | -------------------------------------------------------------------------------- /test/fixtures/exclude.js: -------------------------------------------------------------------------------- 1 | 2 | increment = require('./increment') 3 | 4 | value += increment 5 | -------------------------------------------------------------------------------- /test/fixtures/ext_bar.bar: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | bar: 'Bar!' 3 | }; -------------------------------------------------------------------------------- /test/fixtures/ext_foo.foo: -------------------------------------------------------------------------------- 1 | var bar = require('./ext_bar'); 2 | 3 | module.exports = { 4 | foo: 'Foo!', 5 | bar: bar 6 | }; -------------------------------------------------------------------------------- /test/fixtures/extension.js: -------------------------------------------------------------------------------- 1 | var foo = require('./ext_foo'); 2 | 3 | console.log(foo.foo, foo.bar); -------------------------------------------------------------------------------- /test/fixtures/increment.js: -------------------------------------------------------------------------------- 1 | module.exports = 10 2 | -------------------------------------------------------------------------------- /test/fixtures/normal.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); -------------------------------------------------------------------------------- /test/fixtures/normal2.js: -------------------------------------------------------------------------------- 1 | var gutil = require('gulp-util'); -------------------------------------------------------------------------------- /test/fixtures/shim.js: -------------------------------------------------------------------------------- 1 | console.log('foo'); 2 | require('bar'); 3 | -------------------------------------------------------------------------------- /test/fixtures/trans_bar.coffee: -------------------------------------------------------------------------------- 1 | module.exports = 2 | bar: 'Bar!' -------------------------------------------------------------------------------- /test/fixtures/trans_error.coffee: -------------------------------------------------------------------------------- 1 | # Invalid syntax!!! 2 | ***** 3 | -------------------------------------------------------------------------------- /test/fixtures/trans_foo.coffee: -------------------------------------------------------------------------------- 1 | bar = require './trans_bar' 2 | module.exports = 3 | foo: 'Foo!' 4 | bar: bar -------------------------------------------------------------------------------- /test/fixtures/transform.coffee: -------------------------------------------------------------------------------- 1 | foo = require './trans_foo' 2 | console.log foo.foo, foo.bar -------------------------------------------------------------------------------- /test/main.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var path = require('path'); 3 | var gutil = require('gulp-util'); 4 | var coffeeify = require('coffeeify'); 5 | var expect = require('chai').expect; 6 | var vm = require('vm') 7 | var through = require('through2'); 8 | 9 | var gulpB = require('../'); 10 | var prepare = require('./prepare'); 11 | 12 | function createFakeFile(filename, contents) { 13 | return new gutil.File({ 14 | cwd: process.cwd(), 15 | base: path.join(__dirname, 'fixtures'), 16 | path: path.join(__dirname, 'fixtures', filename), 17 | contents: contents 18 | }); 19 | } 20 | 21 | describe('gulp-browserify', function() { 22 | before(function (done) { 23 | prepare(['normal.js', 'normal2.js', 'exclude.js'], done); 24 | }); 25 | 26 | it('should return files', function(done) { 27 | var fakeFile = createFakeFile('normal.js', new Buffer("var test = 'test';")); 28 | gulpB().once('data', function(bundled){ 29 | expect(bundled.contents).to.exist; 30 | done(); 31 | }).end(fakeFile); 32 | }); 33 | 34 | it('should return a buffer', function(done) { 35 | var fakeFile = createFakeFile('normal.js', new Buffer("var test = 'test';")); 36 | gulpB().once('data', function(bundled){ 37 | expect(bundled.contents).to.be.an.instanceof(Buffer); 38 | done(); 39 | }).end(fakeFile); 40 | }); 41 | 42 | it('should return a vinyl file object', function(done) { 43 | var fakeFile = createFakeFile('normal.js', new Buffer("var test = 'test';")); 44 | gulpB().once('data', function(bundled){ 45 | expect(bundled.cwd).to.be.a('string'); 46 | expect(bundled.base).to.be.a('string'); 47 | expect(bundled.path).to.be.a('string'); 48 | done(); 49 | }).end(fakeFile); 50 | }); 51 | 52 | it('should return a browserify require file', function(done) { 53 | var fakeFile = createFakeFile('normal.js', fs.readFileSync('test/fixtures/normal.js')); 54 | gulpB().once('data', function(bundled) { 55 | expect(bundled.contents.toString()).to.equal(fs.readFileSync('test/expected/normal.js', 'utf8')); 56 | done(); 57 | }).end(fakeFile); 58 | }); 59 | 60 | describe ('it should handle the external option', function() { 61 | it ('when specified as a string', function(done) { 62 | var fakeFile = createFakeFile('external.js', fs.readFileSync('test/fixtures/extension.js')); 63 | var opts = { extensions: ['.foo', '.bar'], external: './ext_bar'}; 64 | gulpB(opts).once('data', function(bundled){ 65 | expect(bundled.contents.toString()).to.match(/foo: 'Foo!'/); 66 | expect(bundled.contents.toString()).to.not.match(/bar: 'Bar!'/); 67 | done(); 68 | }).end(fakeFile); 69 | }); 70 | 71 | it ('when specified as an array', function(done) { 72 | var fakeFile = createFakeFile('external.js', fs.readFileSync('test/fixtures/extension.js')); 73 | var opts = { extensions: ['.foo', '.bar'], external: ['./ext_bar', './ext_foo']}; 74 | gulpB(opts).once('data', function(bundled){ 75 | expect(bundled.contents.toString()).to.not.match(/foo: 'Foo!'/); 76 | expect(bundled.contents.toString()).to.not.match(/bar: 'Bar!'/); 77 | done(); 78 | }).end(fakeFile); 79 | }); 80 | }); 81 | 82 | describe ('it should handle the exclude option', function() { 83 | it ('by throwing an error on invalid requires', function(done) { 84 | var fakeFile = createFakeFile('exclude.js', fs.readFileSync('test/fixtures/exclude.js')) 85 | var opts = { exclude: ['./increment']}; 86 | 87 | gulpB(opts).once('data', function(bundled){ 88 | var sandbox = {value: 20}; 89 | 90 | expect(function() { 91 | vm.runInNewContext(bundled.contents.toString('utf8'), sandbox); 92 | }).to.throw("Cannot find module './increment'"); 93 | 94 | expect(sandbox.value).to.equal(20) 95 | done(); 96 | }).end(fakeFile); 97 | }); 98 | }); 99 | 100 | describe ('it should handle the add option', function() { 101 | it ('by adding contents of given file to bundle', function(done) { 102 | var fakeFile = createFakeFile('add.js', fs.readFileSync('test/fixtures/add.js')); 103 | var opts = { add: ['./add_file'] }; 104 | 105 | gulpB(opts).once('data', function(bundled){ 106 | var sandbox = {addValue: 0}; 107 | 108 | vm.runInNewContext(bundled.contents.toString('utf8'), sandbox); 109 | 110 | expect(sandbox.addValue).to.equal(100); 111 | done(); 112 | }).end(fakeFile); 113 | }); 114 | }); 115 | 116 | describe ('it should handle the ignore option', function() { 117 | it ('when specified as a string', function(done) { 118 | var fakeFile = createFakeFile('ignore.js', fs.readFileSync('test/fixtures/extension.js')); 119 | var opts = { extensions: ['.foo', '.bar'], ignore: './ext_bar'}; 120 | gulpB(opts).once('data', function(bundled){ 121 | expect(bundled.contents.toString()).to.match(/foo: 'Foo!'/); 122 | expect(bundled.contents.toString()).to.not.match(/bar: 'Bar!'/); 123 | done(); 124 | }).end(fakeFile); 125 | }); 126 | it ('when specified as a list', function(done) { 127 | var fakeFile = createFakeFile('ignore.js', fs.readFileSync('test/fixtures/extension.js')); 128 | var opts = { extensions: ['.foo', '.bar'], ignore: ['./ext_foo','./ext_bar']}; 129 | gulpB(opts).once('data', function(bundled){ 130 | expect(bundled.contents.toString()).to.not.match(/foo: 'Foo!'/); 131 | expect(bundled.contents.toString()).to.not.match(/bar: 'Bar!'/); 132 | done(); 133 | }).end(fakeFile); 134 | }); 135 | }); 136 | 137 | describe ('it should handle the require option', function() { 138 | it ('when specified as a string', function(done) { 139 | var fakeFile = createFakeFile('ignore.js', fs.readFileSync('test/fixtures/extension.js')); 140 | var opts = { extensions: ['.foo', '.bar'], require: './ext_bar' }; 141 | gulpB(opts).once('data', function(bundled){ 142 | expect(bundled.contents.toString()).to.match(/foo: 'Foo!'/); 143 | expect(bundled.contents.toString()).to.match(/bar: 'Bar!'/); 144 | expect(bundled.contents.toString()).to.match(/^require=/); 145 | expect(bundled.contents.toString()).to.match(/".\/ext_bar"\:/); 146 | done(); 147 | }).end(fakeFile); 148 | }); 149 | it ('when specified as a list', function(done) { 150 | var fakeFile = createFakeFile('ignore.js', fs.readFileSync('test/fixtures/extension.js')); 151 | var opts = { 152 | extensions: ['.foo', '.bar'], 153 | require: [['./ext_foo', { expose: 'foo' }], ['./ext_bar', { expose: 'bar' }]] 154 | }; 155 | gulpB(opts).once('data', function(bundled){ 156 | expect(bundled.contents.toString()).to.match(/foo: 'Foo!'/); 157 | expect(bundled.contents.toString()).to.match(/bar: 'Bar!'/); 158 | expect(bundled.contents.toString()).to.match(/^require=/); 159 | expect(bundled.contents.toString()).to.match(/"foo"\:/); 160 | expect(bundled.contents.toString()).to.match(/"bar"\:/); 161 | done(); 162 | }).end(fakeFile); 163 | }); 164 | }); 165 | 166 | it('should return a browserify require file without entry point contents', function(done) { 167 | var fakeFile = createFakeFile('normal.js', null); 168 | gulpB().once('data', function(bundled) { 169 | expect(bundled.contents.toString()).to.equal(fs.readFileSync('test/expected/normal.js', 'utf8')); 170 | done(); 171 | }).end(fakeFile); 172 | }); 173 | 174 | it('should bundles multiple entry points', function(done) { 175 | var fakeFile1 = createFakeFile('normal.js', fs.readFileSync('test/fixtures/normal.js')); 176 | var fakeFile2 = createFakeFile('normal2.js', fs.readFileSync('test/fixtures/normal2.js')); 177 | var files = {}; 178 | var B = gulpB().on('data', function(bundled) { 179 | // Order is not guaranteed. Let's keep it with file name. 180 | files[path.basename(bundled.path)] = bundled; 181 | }).on('end', function() { 182 | expect(Object.keys(files).length).to.equal(2); 183 | expect(files['normal.js'].contents.toString()).to.equal(fs.readFileSync('test/expected/normal.js', 'utf8')); 184 | expect(files['normal2.js'].contents.toString()).to.equal(fs.readFileSync('test/expected/normal2.js', 'utf8')); 185 | done(); 186 | }); 187 | B.write(fakeFile1); 188 | B.end(fakeFile2); 189 | }); 190 | 191 | it('should use the file modified through gulp', function(done) { 192 | var fakeFile = createFakeFile('normal.js', new Buffer("var test = 'test';")); 193 | gulpB().once('data', function(bundled){ 194 | expect(bundled.contents.toString()).to.not.equal("var test = 'test';"); 195 | done(); 196 | }).end(fakeFile); 197 | }); 198 | 199 | it('should shim files', function(done) { 200 | var fakeFile = createFakeFile('shim.js', fs.readFileSync('test/fixtures/shim.js')); 201 | var opts = { 202 | shim: { 203 | bar: { 204 | path: 'test/fixtures/bar.js', 205 | exports: 'bar' 206 | } 207 | } 208 | }; 209 | gulpB(opts).once('data', function(bundled){ 210 | expect(bundled.contents.toString()).to.match(/window.bar = \'foobar\'/); 211 | done(); 212 | }).end(fakeFile); 213 | }); 214 | 215 | it('should emit postbundle event', function(done) { 216 | var fakeFile = createFakeFile('normal.js', fs.readFileSync('test/fixtures/normal.js')); 217 | gulpB().once('data', function(bundled) { 218 | expect(bundled.contents).to.exist; 219 | done(); 220 | }).on('postbundle', function(data) { 221 | expect(data.toString()).to.equal(fs.readFileSync('test/expected/normal.js', 'utf8')); 222 | }).end(fakeFile); 223 | }); 224 | 225 | it('should use extensions', function(done) { 226 | var fakeFile = createFakeFile('extension.js', fs.readFileSync('test/fixtures/extension.js')); 227 | var opts = { extensions: ['.foo', '.bar'] }; 228 | gulpB(opts).once('data', function(bundled){ 229 | expect(bundled.contents.toString()).to.match(/foo: 'Foo!'/); 230 | expect(bundled.contents.toString()).to.match(/bar: 'Bar!'/); 231 | done(); 232 | }).end(fakeFile); 233 | }); 234 | 235 | it('should not parse with noParse', function(done) { 236 | var fakeFile = createFakeFile('normal.js', fs.readFileSync('test/fixtures/normal.js')); 237 | var files = []; 238 | gulpB({noParse: 'chai'}).on('data', function(bundled){ 239 | files.push(bundled); 240 | expect(bundled.contents).to.exist; 241 | }).once('end', function(){ 242 | expect(files.length).to.equal(1); 243 | expect(files[0].contents.length).to.equal(581); 244 | done(); 245 | }).end(fakeFile); 246 | }); 247 | 248 | it('should use custom resolve function', function(done) { 249 | // Use new Buffer instead of normal.js contents because normal.js 250 | // requires "chai", and if this test fails (resolve function not used) 251 | // all the chai will be printed to stdout (when .to.match() fails), 252 | // that's annoying, trust me. And new fixture file for this is overkill. 253 | var fakeFile = createFakeFile('fake.js', new Buffer('require("fake");')); 254 | var opts = { 255 | resolve: function(pkg, opts, cb) { 256 | cb(null, path.resolve('./test/fixtures/custom_resolved.js')); 257 | } 258 | }; 259 | gulpB(opts).once('data', function(bundled) { 260 | expect(bundled.contents.toString()).to.match(/resolved content/); 261 | done(); 262 | }).end(fakeFile); 263 | }); 264 | 265 | it('should allow external with buffer', function(done) { 266 | var fakeFile = createFakeFile('normal.js', fs.readFileSync('test/fixtures/normal.js')); 267 | var files = []; 268 | gulpB().on('prebundle', function(bundler) { 269 | bundler.external('chai'); 270 | }).on('data', function(bundled){ 271 | files.push(bundled); 272 | expect(bundled.contents).to.exist; 273 | }).once('end', function(){ 274 | expect(files.length).to.equal(1); 275 | expect(files[0].contents.length).to.equal(504); 276 | done(); 277 | }).end(fakeFile); 278 | }); 279 | 280 | it('should transform files without entry contents', function(done) { 281 | // Don't set file contents. Browserify names stream entry as `fake_xxx.js` 282 | // but coffeify does not work with `.js` files. 283 | // Without contents, gulp-browserify passes file path to browserify 284 | // and browserify can reads file from th e given path. 285 | var fakeFile = createFakeFile('transform.coffee', null); 286 | var opts = { transform: ['coffeeify'], extensions: ['.coffee'] }; 287 | gulpB(opts).once('data', function (bundled) { 288 | expect(bundled.contents.toString()).to.match(/foo: 'Foo!'/); 289 | expect(bundled.contents.toString()).to.match(/bar: 'Bar!'/); 290 | done(); 291 | }).end(fakeFile); 292 | }); 293 | 294 | it('should emit an error when bundle throws a standard error', function(done) { 295 | var fakeFile = createFakeFile('not_found.js', new Buffer('require("--non-existent");')); 296 | gulpB().once('error', function (err) { 297 | expect(err).to.exist; 298 | expect(err).to.be.instanceof(gutil.PluginError); 299 | expect(err.message).to.include('module "--non-existent" not found'); 300 | expect(err.stack).to.exist; 301 | expect(err.plugin).to.eq('gulp-browserify'); 302 | expect(err.name).to.eq('Error'); 303 | done(); 304 | }).end(fakeFile); 305 | }); 306 | 307 | it('should emit an error with file name and line number when bundle throws a syntax error', function(done) { 308 | var fakeFile = createFakeFile('trans_error.coffee', null); 309 | var opts = { transform: ['coffeeify'], extensions: ['.coffee'] }; 310 | gulpB(opts).once('error', function (err) { 311 | expect(err).to.exist; 312 | expect(err).to.be.instanceof(gutil.PluginError); 313 | expect(err.message).to.include('test/fixtures/trans_error.coffee:2'); 314 | expect(err.message).to.include('ParseError: unexpected '); 315 | expect(err.stack).to.exist; 316 | expect(err.plugin).to.eq('gulp-browserify'); 317 | expect(err.name).to.eq('SyntaxError'); 318 | done(); 319 | }).end(fakeFile); 320 | }); 321 | 322 | it('should emit an error when bundle throws a plain string as an error', function(done) { 323 | var fakeFile = createFakeFile('some.js', new Buffer('console.log("something");')); 324 | function stringErrorTransform(file) { 325 | return through(function (chunk, encoding, callback) { 326 | callback('string error!'); 327 | }); 328 | } 329 | var opts = { transform: [stringErrorTransform], extensions: ['.coffee'] }; 330 | gulpB(opts).once('error', function (err) { 331 | expect(err).to.exist; 332 | expect(err).to.be.instanceof(gutil.PluginError); 333 | expect(err.message).to.eq('string error!'); 334 | expect(err.plugin).to.eq('gulp-browserify'); 335 | expect(err.stack).to.exist; 336 | expect(err.name).to.eq('Error'); 337 | done(); 338 | }).end(fakeFile); 339 | }); 340 | }); 341 | -------------------------------------------------------------------------------- /test/prepare.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var path = require('path'); 3 | var browserify = require('browserify'); 4 | 5 | function fixture(filename, cb) { 6 | var entry = path.resolve(__dirname, 'fixtures', filename); 7 | var out = path.resolve(__dirname, 'expected', filename); 8 | 9 | var b = browserify({ entries: entry }); 10 | var opts = { detectGlobals: true }; 11 | var s = b.bundle(opts); 12 | s.on('error', cb); 13 | s.pipe(fs.createWriteStream(out)).on('finish', cb); 14 | } 15 | 16 | // Create bundled files with Browserify for test. 17 | module.exports = function (entries, done) { 18 | var pendings = entries.length; 19 | 20 | function callback(err) { 21 | if (err) { 22 | console.error('Failed to prepare fixture files.'); 23 | console.error(err); 24 | process.exit(1); 25 | } 26 | 27 | if (--pendings <= 0) { 28 | done(); 29 | } 30 | } 31 | 32 | entries.forEach(function (file) { 33 | fixture(file, callback); 34 | }); 35 | }; 36 | --------------------------------------------------------------------------------