Skip to content

Commit

Permalink
Merge pull request #32 from pkrefta/spdy_support
Browse files Browse the repository at this point in the history
[api]SPDY support via node-spdy
  • Loading branch information
pksunkara committed Jul 3, 2012
2 parents a1f0145 + b9c7c06 commit 942cc6d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 18 deletions.
22 changes: 12 additions & 10 deletions README.md
Expand Up @@ -30,14 +30,12 @@ In addition, the response object passed to middlewares listens for a "next" even
``` js
var fs = require('fs'),
union = require('../lib'),
director = require('director'),
favicon = require('./middleware/favicon');
director = require('director');

var router = new director.http.Router();

var server = union.createServer({
before: [
favicon('./favicon.png'),
function (req, res) {
var found = router.dispatch(req, res);
if (!found) {
Expand Down Expand Up @@ -116,6 +114,8 @@ var server = union.createServer({
}).listen(3000);
```

### SPDY enabled server example

# API

## union Static Members
Expand Down Expand Up @@ -149,6 +149,10 @@ Specification
(optional) A value that specifies the certificate and key necessary to create an instance of
`https.Server`.
@option spdy {Object}
(optional) A value that specifies the certificate and key necessary to create an instance of
`spdy.Server`.
@option headers {Object}
(optional) An object representing a set of headers to set in every outgoing response
```
Expand All @@ -169,15 +173,13 @@ var server = union.createServer({
});
```

An example of the `https` option.
An example of the `https` or `spdy` option.

``` js
{
https: {
cert: 'path/to/cert.pem',
key: 'path/to/key.pem',
ca: 'path/to/ca.pem'
}
cert: 'path/to/cert.pem',
key: 'path/to/key.pem',
ca: 'path/to/ca.pem'
}
```

Expand Down Expand Up @@ -310,7 +312,7 @@ All tests are written with [vows][0] and should be run with [npm][1]:

(The MIT License)

Copyright (c) 2010 Nodejitsu Inc. <http://www.twitter.com/nodejitsu>
Copyright (c) 2010-2012 Nodejitsu Inc. <http://www.twitter.com/nodejitsu>

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:

Expand Down
30 changes: 30 additions & 0 deletions examples/simple/spdy.js
@@ -0,0 +1,30 @@
// In order to run this example you need to
// generate local ssl certificate
var union = require('../../lib'),
director = require('director');

var router = new director.http.Router();

var server = union.createServer({
before: [
function (req, res) {
var found = router.dispatch(req, res);
if (!found) {
res.emit('next');
}
}
],
spdy :{
key: './certs/privatekey.pem',
cert: './certs/certificate.pem'
}
});

router.get(/foo/, function () {
this.res.writeHead(200, { 'Content-Type': 'text/plain' })
this.res.end('hello world\n');
});

server.listen(9090, function(){
console.log('union with director running on 9090 with SPDY');
});
40 changes: 32 additions & 8 deletions lib/core.js
@@ -1,5 +1,5 @@
/*
* core.js: Core functionality for the Flatiron HTTP plugin.
* core.js: Core functionality for the Flatiron HTTP (with SPDY support) plugin.
*
* (C) 2011, Nodejitsu Inc.
* MIT LICENSE
Expand Down Expand Up @@ -50,18 +50,42 @@ core.createServer = function (options) {
req.pipe(routingStream);
}

if (options.https) {
if (!options.https.key || !options.https.cert) {
throw new Error('Both `options.https.key` and `options.https.cert` are required.');
//
// both https and spdy requires same params
//
if (options.https || options.spdy) {

if(options.https && options.spdy){
throw new Error('You shouldn\'t be using https and spdy simultaneously.')
}

var key, serverOptions, credentials;

if(options.spdy) {
key = 'spdy';
} else {
key = 'https';
}

serverOptions = options[key];

if (!serverOptions.key || !serverOptions.cert) {
throw new Error('Both options.'+key+'.`key` and options.'+key+'.`cert` are required.');
}

credentials = {
key: fs.readFileSync(options.https.key),
cert: fs.readFileSync(options.https.cert)
key: fs.readFileSync(serverOptions.key),
cert: fs.readFileSync(serverOptions.cert)
};

if (options.https.ca) {
credentials.ca = fs.readFileSync(options.https.ca);
if (serverOptions.ca) {
credentials.ca = fs.readFileSync(serverOptions.ca);
}

if(options.spdy){
// spdy is optional so we require module here rather than on top
var spdy = require('spdy');
return spdy.createServer(credentials, requestHandler);
}

return https.createServer(credentials, requestHandler);
Expand Down

0 comments on commit 942cc6d

Please sign in to comment.