Skip to content

Commit

Permalink
Merge pull request #7 from zerious/master
Browse files Browse the repository at this point in the history
Added HEAD support, sync middlewares, and pattern paths
  • Loading branch information
zerious committed Oct 28, 2014
2 parents a9c35f9 + 8847dc9 commit 55e427a
Show file tree
Hide file tree
Showing 14 changed files with 531 additions and 299 deletions.
21 changes: 0 additions & 21 deletions MIT-LICENSE.md

This file was deleted.

110 changes: 101 additions & 9 deletions README.md
@@ -1,24 +1,25 @@
# Za
# <a href="http://lighter.io/za" style="font-size:40px;text-decoration:none;color:#000"><img src="https://cdn.rawgit.com/lighterio/lighter.io/master/public/za.svg" style="width:90px;height:90px"> Za</a>
[![NPM Version](https://img.shields.io/npm/v/za.svg)](https://npmjs.org/package/za)
[![Downloads](https://img.shields.io/npm/dm/za.svg)](https://npmjs.org/package/za)
[![Build Status](https://img.shields.io/travis/lighterio/za.svg)](https://travis-ci.org/lighterio/za)
[![Code Coverage](https://img.shields.io/coveralls/lighterio/za/master.svg)](https://coveralls.io/r/lighterio/za)
[![Dependencies](https://img.shields.io/david/lighterio/za.svg)](https://david-dm.org/lighterio/za)
[![Support](https://img.shields.io/gratipay/Lighter.io.svg)](https://gratipay.com/Lighter.io/)

[![NPM Version](https://badge.fury.io/js/za.png)](http://badge.fury.io/js/za)
[![Build Status](https://travis-ci.org/lighterio/za.png?branch=master)](https://travis-ci.org/lighterio/za)
[![Code Coverage](https://coveralls.io/repos/lighterio/za/badge.png?branch=master)](https://coveralls.io/r/lighterio/za)
[![Dependencies](https://david-dm.org/lighterio/za.png?theme=shields.io)](https://david-dm.org/lighterio/za)
[![Support](http://img.shields.io/gittip/zerious.png)](https://www.gittip.com/lighterio/)

## TL;DR

Za is a simple web server that exposes an Express-like API and is capable of
handling far more requests per second than Express.

## Getting Started

### Quick Start
Install `za` in your project:

```bash
npm install --save za
```

Use `za` to start an HTTP server:

```javascript
var server = require('./za')();
server.listen(8888);
Expand All @@ -29,6 +30,7 @@ console.log('Visit http://localhost:8888/ for "Hello World!"');

```


## Server and Router

### za()
Expand Down Expand Up @@ -226,6 +228,96 @@ When the multipart parser is finished, the request emits a `"za:finished"`
event. At that time, `request.multipart` (as well as `request.body`) contain
all of the data that was sent in the multipart request.


## Why is it called "Za"?

The term "za" is short for "pizza", and when it comes to web app responses and
pizza, everyone wants fast delivery. (Also, the name was available on NPM).


## Acknowledgements

We would like to thank all of the amazing people who use, support,
promote, enhance, document, patch, and submit comments & issues.
Za couldn't exist without you.

Additionally, huge thanks go to [TUNE](http://www.tune.com) for employing
and supporting [Za](http://lighter.io/za) project maintainers,
and for being an epically awesome place to work (and play).


## MIT License

Copyright (c) 2014 Sam Eubank

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.


## How to Contribute

We welcome contributions from the community and are happy to have them.
Please follow this guide when logging issues or making code changes.

### Logging Issues

All issues should be created using the
[new issue form](https://github.com/lighterio/za/issues/new).
Please describe the issue including steps to reproduce. Also, make sure
to indicate the version that has the issue.

### Changing Code

Code changes are welcome and encouraged! Please follow our process:

1. Fork the repository on GitHub.
2. Fix the issue ensuring that your code follows the
[style guide](http://lighter.io/style-guide).
3. Add tests for your new code, ensuring that you have 100% code coverage.
(If necessary, we can help you reach 100% prior to merging.)
* Run `npm test` to run tests quickly, without testing coverage.
* Run `npm run cover` to test coverage and generate a report.
* Run `npm run report` to open the coverage report you generated.
4. [Pull requests](http://help.github.com/send-pull-requests/) should be made
to the [master branch](https://github.com/lighterio/za/tree/master).

### Contributor Code of Conduct

As contributors and maintainers of Za, we pledge to respect all
people who contribute through reporting issues, posting feature requests,
updating documentation, submitting pull requests or patches, and other
activities.

If any participant in this project has issues or takes exception with a
contribution, they are obligated to provide constructive feedback and never
resort to personal attacks, trolling, public or private harassment, insults, or
other unprofessional conduct.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, edits, issues, and other contributions
that are not aligned with this Code of Conduct. Project maintainers who do
not follow the Code of Conduct may be removed from the project team.

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by opening an issue or contacting one or more of the project
maintainers.

We promise to extend courtesy and respect to everyone involved in this project
regardless of gender, gender identity, sexual orientation, ability or
disability, ethnicity, religion, age, location, native language, or level of
experience.
46 changes: 46 additions & 0 deletions lib/Request.js
@@ -0,0 +1,46 @@
var http = require('http');
var parse = require('./parse');

var proto = http.IncomingMessage.prototype;

proto.header = function (name) {
return this.headers[(name || '').toLowerCase()];
};

if (!proto.hasOwnProperty('cookies')) {
Object.defineProperty(proto, 'cookies', {
enumerable: false,
get: function () {
var cookies = {};
var cookie = this.headers.cookie;
if (cookie) {
cookie.split(/; ?/).forEach(function (pair) {
pair = pair.split('=');
cookies[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
});
}
this.cookies = cookies;
return cookies;
}
});
}

if (!proto.hasOwnProperty('query')) {
Object.defineProperty(proto, 'query', {
enumerable: false,
get: function () {
var query = parse(this.queryString);
this.query = query;
return query;
}
});
}

if (!proto.hasOwnProperty('body')) {
Object.defineProperty(proto, 'body', {
enumerable: false,
get: function () {
return this.multipart || parse(this.buffer);
}
});
}
59 changes: 8 additions & 51 deletions lib/http.js → lib/Response.js
@@ -1,21 +1,18 @@
var http = require('http');
var escape = require('querystring').escape;
var parse = require('./parse');

// TODO: Benchmark zlib performance against node-compress.
var zlib = require('zlib');

var requestProto = http.IncomingMessage.prototype;
var responseProto = http.ServerResponse.prototype;
var proto = http.ServerResponse.prototype;

responseProto.json = function (object) {
proto.json = function (object) {
var json = JSON.stringify(object);
var res = this;
res.setHeader('content-type', 'application/json');
res.send(json);
};

responseProto.send = function (data) {
proto.send = function (data) {
var res = this;
var type = typeof data;
if (type == 'string') {
Expand All @@ -33,7 +30,7 @@ responseProto.send = function (data) {
}
};

responseProto.zip = function (text, preZipped) {
proto.zip = function (text, preZipped) {
// TODO: Determine whether 1e3 is the right threshold.
var res = this;
if (preZipped || (text.length > 1e3)) {
Expand All @@ -46,6 +43,7 @@ responseProto.zip = function (text, preZipped) {
else {
zlib.gzip(text, function (err, zipped) {
if (err) {
console.log(err);
res.end(text);
}
else {
Expand All @@ -60,56 +58,15 @@ responseProto.zip = function (text, preZipped) {
res.end(text);
};

responseProto.cookie = function (name, value, options) {
proto.cookie = function (name, value, options) {
var res = this;
res.setHeader('set-cookie', name + '=' + escape(value));
};

responseProto.redirect =
responseProto.redirect || function (location) {
proto.redirect =
proto.redirect || function (location) {
var res = this;
res.statusCode = 302;
res.setHeader('location', location);
res.end();
};

requestProto.header = function (name) {
return this.headers[(name || '').toLowerCase()];
};

if (!requestProto.hasOwnProperty('cookies')) {
Object.defineProperty(requestProto, 'cookies', {
get: function () {
var cookies = {};
var cookie = this.headers.cookie;
if (cookie) {
cookie.split(/; ?/).forEach(function (pair) {
pair = pair.split('=');
cookies[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
});
}
this.cookies = cookies;
return cookies;
}
});
}

if (!requestProto.hasOwnProperty('query')) {
Object.defineProperty(requestProto, 'query', {
enumerable: false,
get: function () {
var query = parse(this.queryString);
this.query = query;
return query;
}
});
}

if (!requestProto.hasOwnProperty('body')) {
Object.defineProperty(requestProto, 'body', {
enumerable: false,
get: function () {
return this.multipart || parse(this.buffer);
}
});
}

0 comments on commit 55e427a

Please sign in to comment.