├── .gitignore ├── test ├── fixtures │ ├── subdirectory │ │ └── noop.js │ ├── counter.js │ └── counter-syntax-error.js └── middleware.js ├── package.json ├── lib ├── logger.js └── cache.js ├── README.md ├── index.js └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | test/_cache 4 | -------------------------------------------------------------------------------- /test/fixtures/subdirectory/noop.js: -------------------------------------------------------------------------------- 1 | exports.default = function () { 2 | }; 3 | -------------------------------------------------------------------------------- /test/fixtures/counter.js: -------------------------------------------------------------------------------- 1 | // Basic ES6 file for Babel to operate on 2 | 3 | class MyCounter { 4 | static get hello() { return "World"; } 5 | 6 | constructor() { 7 | this._count = 0; 8 | } 9 | 10 | inc() { 11 | this._count++; 12 | } 13 | 14 | dec() { 15 | this._count--; 16 | } 17 | 18 | get count() { 19 | return this._count; 20 | } 21 | } 22 | 23 | export default MyCounter; 24 | -------------------------------------------------------------------------------- /test/fixtures/counter-syntax-error.js: -------------------------------------------------------------------------------- 1 | // Basic ES6 file for Babel to operate on 2 | 3 | class MyCounter { 4 | static hello = "World" 5 | 6 | constructor() { 7 | this._count = 0; 8 | } 9 | 10 | inc: function() { 11 | this._count++; 12 | } 13 | 14 | dec() { 15 | this._count--; 16 | } 17 | 18 | get count() { 19 | return this._count; 20 | } 21 | } 22 | 23 | export default MyCounter; 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-middleware", 3 | "version": "0.3.4", 4 | "description": "Express/Connect middleware to pre-process requested JS files through Babel", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha --reporter spec --check-leaks test/" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/mralex/babel-middleware" 12 | }, 13 | "keywords": [ 14 | "babel", 15 | "es6", 16 | "connect", 17 | "express", 18 | "middleware" 19 | ], 20 | "author": "Alex Roberts ", 21 | "license": "Apache 2.0", 22 | "bugs": { 23 | "url": "https://github.com/mralex/babel-middleware/issues" 24 | }, 25 | "homepage": "https://github.com/mralex/babel-middleware", 26 | "dependencies": { 27 | "babel-core": "^6.9.1", 28 | "micromatch": "^2.3.10", 29 | "mkdirp": "^0.5.1", 30 | "rimraf": "^2.5.3" 31 | }, 32 | "devDependencies": { 33 | "chai": "^3.4.1", 34 | "express": "^4.13.3", 35 | "mocha": "^2.3.4", 36 | "sleep": "^3.0.0", 37 | "supertest": "^1.1.0" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /lib/logger.js: -------------------------------------------------------------------------------- 1 | /** 2 | * A basic logging module 3 | */ 4 | 5 | function Logger(level) { 6 | this.level = Logger.LEVELS[level]; 7 | } 8 | 9 | Logger.prototype = { 10 | _shouldLog: function (level) { 11 | return this.level <= Logger.LEVELS[level]; 12 | }, 13 | debug: function () { 14 | if (this._shouldLog('debug')) { 15 | console.log.apply(undefined, arguments); 16 | } 17 | }, 18 | info: function () { 19 | if (this._shouldLog('info')) { 20 | console.log.apply(undefined, arguments); 21 | } 22 | }, 23 | warn: function () { 24 | if (this._shouldLog('warn')) { 25 | console.error.apply(undefined, arguments); 26 | } 27 | }, 28 | error: function () { 29 | if (this._shouldLog('error')) { 30 | console.error.apply(undefined, arguments); 31 | } 32 | }, 33 | critical: function () { 34 | if (this._shouldLog('critical')) { 35 | console.error.apply(undefined, arguments); 36 | } 37 | } 38 | }; 39 | 40 | Logger.LEVELS = { 41 | 'debug': 0, 42 | 'info': 1, 43 | 'warn': 2, 44 | 'error': 3, 45 | 'critical': 9, 46 | 'none': 10 47 | }; 48 | 49 | module.exports = Logger; 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | babel-middleware 2 | ================ 3 | 4 | Simple Express/Connect middleware to automatically transpile JavaScript files 5 | from ES2015+ to ES5 via Babel, and cache the results to memory or the 6 | file-system as desired. 7 | 8 | Usage 9 | ===== 10 | ```javascript 11 | var express = require('express'); 12 | var babel = require('babel-middleware'); 13 | var app = express(); 14 | 15 | app.use('/js/', babel({ 16 | srcPath: 'app/js', 17 | cachePath: __dirname + '/_cache' 18 | babelOptions: { 19 | presets: ['es2015'] 20 | } 21 | })); 22 | 23 | app.listen(3001); 24 | ``` 25 | 26 | Options 27 | ======= 28 | 29 | ### `srcPath: '/path/to/js/'` 30 | An absolute or relative path to the input source. This option is required. 31 | 32 | ### `cachePath: '/path/to/cache/'|'memory'` 33 | Use either _memory_ for an in-memory cache; or a path to the desired cache directory (it does not need to exist when the app starts). 34 | 35 | Default: _memory_ 36 | 37 | ### `exclude: ['production/example/*.js']` 38 | An array of path globs to exclude from transpiling and caching. Returns the originally requested file. See [Micromatch documentation](https://www.npmjs.com/package/micromatch) for globbing examples. Exclusions do not match against `srcPath`. 39 | 40 | Default: _[]_ 41 | 42 | ### `babelOptions: {}` 43 | An options object passed into `babel.transformFile`. See [Babel documentation](https://babeljs.io/docs/usage/options/) for usage. 44 | 45 | ### `debug: true|false` 46 | Print debug output. 47 | 48 | Default: _false_ 49 | 50 | ### `consoleErrors: true|false` 51 | Print errors to the web console. 52 | 53 | Default: _false_ 54 | 55 | ### `logLevel: debug|info|warn|error|critical|none 56 | Minimum log level to print to the server console. 57 | Left is lowest, right highest. 58 | 59 | Default: _none_ 60 | 61 | LICENSE 62 | ======= 63 | 64 | Apache 2.0. 65 | -------------------------------------------------------------------------------- /lib/cache.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var mkdirp = require('mkdirp'); 3 | var rimraf = require('rimraf'); 4 | 5 | function Cache(cachePath, logger, options) { 6 | this.cachePath = cachePath; 7 | this.isDiskCache = cachePath !== 'memory'; 8 | this.logger = logger; 9 | 10 | if (options && options.freshCache) { 11 | this.removeCacheDirectory(); 12 | } 13 | 14 | this.ensureCacheDirectoryExists(); 15 | 16 | this.cacheMap = {}; 17 | } 18 | 19 | Cache.prototype = { 20 | removeCacheDirectory: function () { 21 | if (this.isDiskCache) { 22 | try { 23 | rimraf.sync(this.cachePath); 24 | } catch (err) { 25 | this.logger.warn('Error deleting cache directory ' + this.cachePath + ': ' + err); 26 | } 27 | } 28 | }, 29 | 30 | ensureCacheDirectoryExists: function () { 31 | if (this.isDiskCache) { 32 | try { 33 | mkdirp.sync(this.cachePath); 34 | } catch (err) { 35 | this.logger.warn('Error creating cache path ' + cachePath + ': ' + err); 36 | } 37 | } 38 | }, 39 | 40 | store: function (path, data) { 41 | var cacheMap = this.cacheMap; 42 | this.cacheMap[path] = data; 43 | 44 | if (this.isDiskCache) { 45 | this.ensureCacheDirectoryExists(); 46 | fs.writeFile(path, data, function (err) { 47 | if (err) { 48 | this.logger.warn('Error saving ' + path + ': ' + err); 49 | delete cacheMap[path]; 50 | } 51 | }); 52 | } 53 | }, 54 | 55 | get: function (path) { 56 | if (! this.isDiskCache && this.cacheMap[path]) { 57 | return this.cacheMap[path]; 58 | } else if (this.isDiskCache) { 59 | var data; 60 | 61 | try { 62 | data = fs.readFileSync(path); 63 | } catch (err) { 64 | this.logger.warn('Error reading ' + path + ': ' + err); 65 | return null; 66 | } 67 | 68 | this.cacheMap[path] = data; 69 | return data; 70 | } 71 | }, 72 | 73 | remove: function (path) { 74 | delete this.cacheMap[path]; 75 | 76 | if (this.isDiskCache) { 77 | try { 78 | fs.unlinkSync(path); 79 | } catch (err) { 80 | this.logger.warn('Error removing ' + path + ': ' + err); 81 | } 82 | } 83 | }, 84 | 85 | isCached: function (path) { 86 | return !! this.get(path); 87 | } 88 | }; 89 | 90 | module.exports = Cache; 91 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | var babel = require('babel-core'); 3 | var Cache = require('./lib/cache'); 4 | var crypto = require('crypto'); 5 | var fs = require('fs'); 6 | var Logger = require('./lib/logger'); 7 | var micromatch = require('micromatch'); 8 | var path = require('path'); 9 | 10 | function lastModifiedHash(path, stats) { 11 | var mtime = stats.mtime.getTime(); 12 | 13 | return crypto 14 | .createHash('md5') 15 | .update(mtime + '-' + path) 16 | .digest('hex'); 17 | } 18 | 19 | function getFileStats(src) { 20 | var stats; 21 | try { 22 | stats = fs.lstatSync(src); 23 | } catch(e) { 24 | // path not found 25 | return null; 26 | } 27 | 28 | if (! stats || ! stats.isFile()) { 29 | // not a file 30 | return null; 31 | } 32 | 33 | return stats; 34 | } 35 | 36 | function isExcluded(path, exclude) { 37 | if (exclude.length) { 38 | return micromatch.any(path.replace(/^\/+|\/+$/g, ''), exclude); 39 | } 40 | 41 | return false; 42 | } 43 | 44 | module.exports = function(options) { 45 | options = options || {}; 46 | 47 | var srcPath = options.srcPath; 48 | var cachePath = options.cachePath || 'memory'; 49 | var exclude = options.exclude || []; 50 | var webConsoleErrors = options.consoleErrors || false; 51 | var logger = new Logger(options.logLevel || 'none'); 52 | 53 | // filename to last known hash map 54 | var hashMap = {}; 55 | 56 | var cache = new Cache(cachePath, logger, options); 57 | 58 | var babelOptions = options.babelOptions || { presets: [] }; 59 | 60 | babelOptions.highlightCode = false; 61 | 62 | function handleError(res, error) { 63 | var errOutput = String(error).replace(/\'/g, '\\\'').replace(/\"/g, '\\\"'); 64 | 65 | logger.error( 66 | 'Babel parsing error from babel-middleware' + 67 | '\n "' + errOutput + '"', '\n' + error.codeFrame 68 | ); 69 | 70 | if (webConsoleErrors) { 71 | res.send( 72 | '/* Babel parsing error from babel-middleware */' + 73 | '\n /* See error console output for details. */' + 74 | '\n var output = ' + JSON.stringify(error) + 75 | '\n console.error("' + errOutput + '\\n", output.codeFrame)' 76 | ); 77 | } else { 78 | res.status(500).send(error); 79 | } 80 | 81 | res.end(); 82 | } 83 | 84 | function pathForHash(hash) { 85 | return path.resolve(cachePath + '/' + hash + '.js'); 86 | } 87 | 88 | return function(req, res, next) { 89 | if (isExcluded(req.path, exclude)) { 90 | logger.debug('Excluded: %s (%s)', req.path, exclude); 91 | res.append('X-Babel-Cache', false); 92 | return next(); 93 | } 94 | 95 | var src = path.resolve(srcPath + '/' + req.path); // XXX Need the correct path 96 | var stats = getFileStats(src); 97 | if (! stats) { 98 | // not a valid file, pass to the next middleware 99 | return next(); 100 | } 101 | 102 | var hash = lastModifiedHash(src, stats); 103 | var lastKnownHash = hashMap[src]; 104 | 105 | // Clean up cached resources any time the 106 | // hash has changed. 107 | if (lastKnownHash && lastKnownHash !== hash) { 108 | cache.remove(pathForHash(lastKnownHash)); 109 | } 110 | 111 | logger.debug('Preparing: %s (%s)', src, hash); 112 | 113 | res.append('X-Babel-Cache', true); 114 | res.append('X-Babel-Cache-Hash', hash); 115 | 116 | var hashPath = pathForHash(hash); 117 | 118 | var code = cache.get(hashPath); 119 | if (code) { 120 | hashMap[src] = hash; 121 | res.append('Content-Type', 'application/javascript'); 122 | res.append('X-Babel-Cache-Hit', true); 123 | logger.debug('Serving (cached): %s', src); 124 | res.write(code); 125 | res.end(); 126 | return; 127 | } 128 | 129 | // expect an X-Babel-Cache-Hit header even on a parse error. 130 | res.append('X-Babel-Cache-Hit', false); 131 | 132 | var result; 133 | try { 134 | result = babel.transformFileSync(src, babelOptions); 135 | } catch(e) { 136 | handleError(res, e); 137 | return; 138 | } 139 | 140 | code = result.code; 141 | hashMap[src] = hash; 142 | 143 | cache.store(hashPath, code); 144 | logger.debug('Serving (uncached): %s', src); 145 | res.append('Content-Type', 'application/javascript'); 146 | res.write(code); 147 | res.end(); 148 | }; 149 | }; 150 | -------------------------------------------------------------------------------- /test/middleware.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'), 2 | rimraf = require('rimraf'), 3 | sleep = require('sleep'), 4 | request = require('supertest'), 5 | expect = require('chai').expect, 6 | express = require('express'), 7 | babel = require('babel-core'), 8 | babelMiddleware = require('../index'); 9 | 10 | function transformFile(file) { 11 | return babel.transformFileSync(file, { presets: [] }).code; 12 | } 13 | 14 | function baseSuite() { 15 | 16 | describe('a fresh cache', function() { 17 | it('hits the proxy', function(done) { 18 | request(this.app) 19 | .get('/counter.js') 20 | .expect('Content-Type', 'application/javascript') 21 | .expect('X-Babel-Cache', 'true') 22 | .expect(200, done); 23 | }); 24 | 25 | it("doesn't get a cache hit", function(done) { 26 | request(this.app) 27 | .get('/counter.js') 28 | .expect('Content-Type', 'application/javascript') 29 | .expect('X-Babel-Cache-Hit', 'false') 30 | .expect(200, done); 31 | }); 32 | 33 | it('returns a transpiled response', function(done) { 34 | var expectedCode = transformFile(__dirname + '/fixtures/counter.js'); 35 | 36 | request(this.app) 37 | .get('/counter.js') 38 | .expect('Content-Type', 'application/javascript') 39 | .expect('X-Babel-Cache', 'true') 40 | .expect(200) 41 | .expect(expectedCode, done); 42 | }); 43 | }); 44 | 45 | describe('a warm cache', function() { 46 | beforeEach(function(done) { 47 | request(this.app).get('/counter.js').end(done); 48 | }); 49 | 50 | it('should get a cache hit', function(done) { 51 | request(this.app) 52 | .get('/counter.js') 53 | .expect('Content-Type', 'application/javascript') 54 | .expect('X-Babel-Cache-Hit', 'true') 55 | .expect(200, done); 56 | }); 57 | 58 | it('returns the transpiled response', function(done) { 59 | var expectedCode = transformFile(__dirname + '/fixtures/counter.js'); 60 | 61 | request(this.app) 62 | .get('/counter.js') 63 | .expect('Content-Type', 'application/javascript') 64 | .expect('X-Babel-Cache', 'true') 65 | .expect('X-Babel-Cache-Hit', 'true') 66 | .expect(200) 67 | .expect(expectedCode, done); 68 | }); 69 | }); 70 | 71 | describe('parsing a syntax error', function() { 72 | it('responds with a 500', function(done) { 73 | request(this.app) 74 | .get('/counter-syntax-error.js') 75 | .expect(500, done); 76 | }); 77 | 78 | describe('caching behaviour', function() { 79 | beforeEach(function(done) { 80 | request(this.app).get('/counter-syntax-error.js').end(done); 81 | }); 82 | 83 | it('does not cache', function(done) { 84 | request(this.app) 85 | .get('/counter-syntax-error.js') 86 | .expect('X-Babel-Cache-Hit', 'false') 87 | .expect(500, done); 88 | }); 89 | }); 90 | }); 91 | 92 | describe('modifying a cached file', function() { 93 | beforeEach(function(done) { 94 | this.filename = __dirname + '/fixtures/test_output.js'; 95 | this.url = '/test_output.js'; 96 | 97 | fs.writeFileSync(this.filename, 'console.log("Hello, world");'); 98 | request(this.app).get(this.url).end(function(err, res) { 99 | this.originalBody = res.body; 100 | fs.writeFileSync(this.filename, 'console.log("The world changed");'); 101 | done(); 102 | }.bind(this)); 103 | 104 | sleep.sleep(1); 105 | }); 106 | 107 | afterEach(function() { 108 | fs.unlinkSync(this.filename); 109 | }); 110 | 111 | it('does not return a cached response', function(done) { 112 | request(this.app) 113 | .get(this.url) 114 | .expect('Content-Type', 'application/javascript') 115 | .expect('X-Babel-Cache-Hit', 'false') 116 | .expect(200, done); 117 | }); 118 | 119 | it('does not return the original code', function(done) { 120 | request(this.app) 121 | .get(this.url) 122 | .end(function(err, res) { 123 | expect(res.body).not.to.equal(this.originalBody); 124 | done(); 125 | }); 126 | }); 127 | }); 128 | } 129 | 130 | describe('middleware', function() { 131 | it('should exist', function() { 132 | expect(babelMiddleware).to.exist; 133 | }); 134 | 135 | describe('in-memory cache', function() { 136 | beforeEach(function() { 137 | this.app = express(); 138 | this.app.use(babelMiddleware({ 139 | cachePath: 'memory', 140 | srcPath: __dirname + '/fixtures' 141 | })); 142 | }); 143 | 144 | baseSuite(); 145 | }); 146 | 147 | describe('filesystem cache configuration', function() { 148 | afterEach(function() { 149 | rimraf.sync(this.cachePath, {}, function() {}); 150 | }); 151 | 152 | it('handles a present cache directory', function() { 153 | this.cachePath = __dirname + '/_cache'; 154 | rimraf.sync(this.cachePath, {}, function() {}); 155 | fs.mkdirSync(this.cachePath); 156 | 157 | this.app = express(); 158 | 159 | var initFn = function() { 160 | this.app.use(babelMiddleware({ 161 | cachePath: this.cachePath, 162 | srcPath: __dirname + '/fixtures' 163 | })); 164 | }.bind(this); 165 | 166 | expect(initFn).to.not.throw(); 167 | }); 168 | }); 169 | 170 | describe('filesystem cache', function() { 171 | beforeEach(function() { 172 | this.cachePath = __dirname + '/_cache'; 173 | this.app = express(); 174 | this.app.use(babelMiddleware({ 175 | cachePath: this.cachePath, 176 | srcPath: __dirname + '/fixtures' 177 | })); 178 | }); 179 | 180 | afterEach(function() { 181 | rimraf.sync(this.cachePath, {}, function() {}); 182 | }); 183 | 184 | function testFileGetsCached() { 185 | it('caches a file', function(done) { 186 | request(this.app) 187 | .get('/counter.js') 188 | .end(function(err, res) { 189 | var hash = res.header['x-babel-cache-hash']; 190 | var filename = this.cachePath + '/' + hash + '.js'; 191 | 192 | expect(function() { 193 | fs.lstatSync(filename); 194 | }).to.not.throw(); 195 | 196 | done(); 197 | }.bind(this)); 198 | }); 199 | } 200 | 201 | testFileGetsCached(); 202 | baseSuite(); 203 | 204 | describe('on restart', function() { 205 | beforeEach(function(done) { 206 | request(this.app).get('/counter.js').end(done); 207 | this.app = express(); 208 | this.app.use(babelMiddleware({ 209 | cachePath: this.cachePath, 210 | srcPath: __dirname + '/fixtures' 211 | })); 212 | }); 213 | 214 | it('uses previously cached assets', function(done) { 215 | request(this.app) 216 | .get('/counter.js') 217 | .expect('Content-Type', 'application/javascript') 218 | .expect('X-Babel-Cache-Hit', 'true') 219 | .expect(200, done); 220 | }); 221 | }); 222 | 223 | describe('if the cache goes away', function() { 224 | beforeEach(function(done) { 225 | request(this.app).get('/counter.js').end(done); 226 | }); 227 | 228 | function deletedFileTests() { 229 | it('loads an uncached version again', function(done) { 230 | request(this.app) 231 | .get('/counter.js') 232 | .expect('Content-Type', 'application/javascript') 233 | .expect('X-Babel-Cache-Hit', 'false') 234 | .expect(200, done); 235 | }); 236 | 237 | testFileGetsCached(); 238 | } 239 | 240 | describe('if only the files are deleted', function() { 241 | beforeEach(function() { 242 | rimraf.sync(this.cachePath + '/*', {}, function() {}); 243 | }); 244 | 245 | deletedFileTests(); 246 | }); 247 | 248 | describe('if the cache directory is deleted', function() { 249 | beforeEach(function() { 250 | rimraf.sync(this.cachePath, {}, function() {}); 251 | }); 252 | 253 | deletedFileTests(); 254 | }); 255 | }); 256 | }); 257 | 258 | describe('excluding files', function() { 259 | beforeEach(function() { 260 | var root = __dirname + '/fixtures'; 261 | this.app = express(); 262 | this.app.use(babelMiddleware({ 263 | cachePath: 'memory', 264 | srcPath: root, 265 | exclude: ['*syntax*'] 266 | })); 267 | this.app.use(express.static(root)); 268 | }); 269 | 270 | it('returns the original file on request', function(done) { 271 | var expectedCode = fs.readFileSync(__dirname + '/fixtures/counter-syntax-error.js', 'utf8'); 272 | 273 | request(this.app) 274 | .get('/counter-syntax-error.js') 275 | .expect('Content-Type', 'application/javascript') 276 | .expect('X-Babel-Cache', 'false') 277 | .expect(200) 278 | .expect(expectedCode, done); 279 | }); 280 | }); 281 | 282 | describe('missing and invalid filenames', function () { 283 | describe('a missing file', function() { 284 | beforeEach(function() { 285 | this.app = express(); 286 | this.app.use(babelMiddleware({ 287 | cachePath: 'memory', 288 | srcPath: __dirname + '/fixtures' 289 | })); 290 | }); 291 | 292 | it('404s', function(done) { 293 | request(this.app) 294 | .get('/missing-file.js') 295 | .expect(404, done); 296 | }); 297 | }); 298 | 299 | describe('a subdirectory', function() { 300 | beforeEach(function() { 301 | this.app = express(); 302 | this.app.use(babelMiddleware({ 303 | cachePath: 'memory', 304 | srcPath: __dirname + '/fixtures' 305 | })); 306 | }); 307 | 308 | it('404s', function(done) { 309 | request(this.app) 310 | .get('/subdirectory') 311 | .expect(404, done); 312 | }); 313 | }); 314 | }); 315 | }); 316 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------