Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Apr 3, 2014
0 parents commit 7aab89a
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 0 deletions.
66 changes: 66 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db

# Node.js #
###########
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules
npm-debug.log

# Git #
#######
*.orig
*.BASE.*
*.BACKUP.*
*.LOCAL.*
*.REMOTE.*

# Components #
##############

/build
/components
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.11"
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Component Koa Middleware

Middleware for Component to be used with Koa. This will rebuild the `.js` and `.css` build files on every request.

This has the exact same usage as [component-middleware](https://github.com/component/middleware.js).

## License

(The MIT License)

Copyright (c) 2014 segmentio <team@segment.io>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
58 changes: 58 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

var Build = require('component-build');
var Resolve = require('component-resolver');

module.exports = function (build, options) {
if (typeof build === 'object') {
options = build;
build = Build;
} else if (typeof build !== 'function') {
build = Build;
}

options = options || {};
// always install instead of erroring!
options.install = true;
// add require() by default
if (options.require == null) options.require = true;

var root = options.root || process.cwd();
var resolving = false;
var queue = [];
var path = options.path || '/build';
var re = new RegExp('^' + path + '.(js|css)$');

return function* componentMiddleware(next) {
var m = re.exec(this.request.path);
if (!m) return yield* next;

var tree = yield resolve;
var _build = build(tree, options);

switch (m[1]) {
case 'js':
this.response.body = yield _build.scripts.bind(_build);
this.response.set('Cache-Control', 'private, no-cache');
this.response.type = 'js';
return;
case 'css':
this.response.body = yield _build.styles.bind(_build);
this.response.set('Cache-Control', 'private, no-cache');
this.response.type = 'css';
return;
}
}

// this is so you don't resolve two times at the same time.
// overoptimization, but hey.
function resolve(done) {
if (resolving) return queue.push(done);
resolving = true;

Resolve(root, options, function (err, tree) {
resolving = false;
while (queue.length) queue.shift()(err, tree);
done(err, tree);
});
}
}
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "component-koa",
"description": "component building middleware for koa",
"version": "1.0.0",
"author": {
"name": "Jonathan Ong",
"email": "me@jongleberry.com",
"url": "http://jongleberry.com",
"twitter": "https://twitter.com/jongleberry"
},
"license": "MIT",
"repository": "component/koa.js",
"dependencies": {
"component-resolver": "^1.0.0",
"component-build": "^1.0.0"
},
"devDependencies": {
"koa": "*",
"mocha": "*",
"supertest": "*"
},
"scripts": {
"test": "mocha --harmony-generators --reporter spec --timeout 10s --bail test/index.js"
}
}
1 change: 1 addition & 0 deletions test/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('emitter');
8 changes: 8 additions & 0 deletions test/component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test",
"dependencies": {
"necolas/normalize.css": "*",
"component/emitter": "*"
},
"scripts": ["client.js"]
}
37 changes: 37 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

var request = require('supertest');
var koa = require('koa');
var vm = require('vm');

var serve = require('..')({
root: __dirname
});

var server = koa().use(serve).listen();

it('should serve js', function (done) {
request(server)
.get('/build.js')
.expect('content-type', 'application/javascript')
.expect(200, function (err, res) {
if (err) return err;

var js = res.text;
var ctx = vm.createContext();
vm.runInContext(js, ctx);
done();
})
})

it('should serve css', function (done) {
request(server)
.get('/build.css')
.expect('content-type', 'text/css; charset=utf-8')
.expect(200, done);
})

it('should 404 otherwise', function (done) {
request(server)
.get('/kljalskdjfasdf')
.expect(404, done);
})

0 comments on commit 7aab89a

Please sign in to comment.