Comparing version 2.0.0-pre5 to 2.0.0-pre6
@@ -43,3 +43,3 @@ 'use strict'; | ||
if (argv.v || argv.version) { | ||
return console.log(require('../package.json').version); | ||
return console.log(quinn.version); | ||
} | ||
@@ -46,0 +46,0 @@ |
@@ -18,1 +18,9 @@ 'use strict'; | ||
} module.exports.setInRequestContext = setInRequestContext; | ||
function lazyCalcForRequest(key, fn, req) { | ||
var value = getFromRequestContext(key); | ||
if (value === undefined) { | ||
setInRequestContext(key, value = fn(req)); | ||
} | ||
return value; | ||
} module.exports.lazyCalcForRequest = lazyCalcForRequest; |
@@ -7,12 +7,20 @@ 'use strict'; | ||
var partial = require('lodash').partial; | ||
var lazyCalcForRequest = require('./context').lazyCalcForRequest; | ||
function _getCookies(req) { | ||
var cookieHeader = req.headers.cookie; | ||
return ( | ||
typeof cookieHeader !== 'string' ? {} | ||
: parseCookies(req.headers.cookie) | ||
); | ||
} | ||
var getCookies = partial(lazyCalcForRequest, 'cookies', _getCookies); | ||
module.exports.getCookies = getCookies; | ||
function getCookie(req, name) { | ||
var cookieHeader = req.headers.cookie; | ||
if (!req._parsedCookies) { | ||
req._parsedCookies = ( | ||
typeof cookieHeader !== 'string' ? {} | ||
: parseCookies(req.headers.cookie) | ||
); | ||
} | ||
var cookies = req._parsedCookies; | ||
var cookies = getCookies(req); | ||
return cookies[name]; | ||
@@ -19,0 +27,0 @@ } module.exports.getCookie = getCookie; |
'use strict'; | ||
var Promise = require('bluebird'); | ||
var partial = require('lodash').partial; | ||
var resolveDeep = require('resolve-deep'); | ||
var Debug = require('debug'); | ||
var respond = require('quinn.respond'); | ||
var respond = require('quinn-respond'); | ||
var mod$0 = require('quinn-respond');var notFound = mod$0.notFound;var internalServerError = mod$0.internalServerError; | ||
var getRequestContextNS = require('./context').getRequestContextNS; | ||
var debug = Debug('quinn:core'); | ||
function pipeTo(target, src) { | ||
@@ -17,24 +21,13 @@ if (src === undefined) return; | ||
function defaultErrorHandler(req, err) { | ||
if (err) { | ||
return ServerError(err.stack); | ||
} else { | ||
return NotFound(("Cannot " + req.method + " " + req.url + "\n")); | ||
} | ||
return err ? | ||
internalServerError(err.stack) : | ||
notFound(("Cannot " + req.method + " " + req.url + "\n")); | ||
} | ||
function defaultFatalHandler(req, err) { | ||
setTimeout(function() { | ||
throw err; | ||
}); | ||
setTimeout(function() { throw err; }); | ||
} | ||
function callIfUndefined(fn, value) { | ||
if (value === undefined) { | ||
return fn(); | ||
} else { | ||
return value; | ||
} | ||
} | ||
module.exports = function quinn(handler, errorHandler, fatalHandler) { | ||
function quinn(handler, errorHandler, fatalHandler) { | ||
debug('init'); | ||
if (typeof errorHandler !== 'function') { | ||
@@ -48,29 +41,21 @@ errorHandler = undefined; | ||
return getRequestContextNS().bind(function handleRequest(req, res, pass) { | ||
var hasPass = typeof pass === 'function'; | ||
return getRequestContextNS().bind(function handleRequest(req, res) { | ||
var url = req.url; | ||
debug('handleRequest', url); | ||
var gracefulError = ( | ||
(errorHandler && partial(errorHandler, req)) || | ||
(!hasPass && partial(defaultErrorHandler, req)) || | ||
undefined | ||
partial(defaultErrorHandler, req) | ||
); | ||
var gracefulRespond = ( | ||
(gracefulError && partial(callIfUndefined, gracefulError)) || | ||
undefined | ||
); | ||
function gracefulRespond(res) { | ||
debug('found? %j', res !== undefined, url); | ||
return res === undefined ? gracefulError() : res; | ||
} | ||
var forward = ( | ||
(hasPass && partial(callIfUndefined, pass)) || | ||
undefined | ||
); | ||
Promise.try(handler, [req]) | ||
.then( | ||
gracefulRespond, | ||
gracefulError | ||
) | ||
runRequestHandlerRaw(handler, req, {}) | ||
.then(gracefulRespond, gracefulError) | ||
.then(respond) | ||
.then(function(r) {return r.resolved();}) | ||
.then(partial(pipeTo, res)) | ||
.then(forward, pass) | ||
.catch(partial(fatalHandler, req)) | ||
@@ -80,9 +65,12 @@ .catch(partial(defaultFatalHandler, req)); | ||
} | ||
quinn.version = require('../package.json').version; | ||
module.exports = quinn; | ||
function ServerError(props) { | ||
return respond(props).status(500); | ||
} module.exports.ServerError = ServerError; | ||
function runRequestHandlerRaw(handler, req, params) { | ||
return resolveDeep(params).then(partial(handler, req)); | ||
} module.exports.runRequestHandlerRaw = runRequestHandlerRaw; | ||
function NotFound(props) { | ||
return respond(props).status(404); | ||
} module.exports.NotFound = NotFound; | ||
function runRequestHandler(handler, req, params) { | ||
return runRequestHandlerRaw(handler, req, params) | ||
.then(respond); | ||
} module.exports.runRequestHandler = runRequestHandler; |
@@ -5,7 +5,6 @@ 'use strict'; | ||
var router = require('quinn.router'); | ||
var respond = require('quinn.respond'); | ||
var router = require('quinn-router'); | ||
var respond = require('quinn-respond'); | ||
var route = router.route; | ||
var getParam = router.getParam; | ||
@@ -19,4 +18,4 @@ var app = route(function(router) { | ||
GET('/hello/{name}', function() { | ||
return 'Hello, ' + getParam('name'); | ||
GET('/hello/{name}', function(req, params) { | ||
return 'Hello, ' + params.name; | ||
}); | ||
@@ -36,4 +35,3 @@ | ||
text: text, | ||
now: new Date(), | ||
r: [ /foo/, /bar/g ] | ||
now: new Date() | ||
}; | ||
@@ -40,0 +38,0 @@ }) |
@@ -9,5 +9,4 @@ /** | ||
import {getParam} from 'quinn.router'; | ||
import respond from 'quinn.respond'; | ||
import {BufferBody} from 'quinn.respond'; | ||
import respond from 'quinn-respond'; | ||
import {BufferBody} from 'quinn-respond'; | ||
@@ -78,6 +77,6 @@ function loadPost(postId) { | ||
export function showPost() { | ||
export function showPost(req, params) { | ||
return MyFancyLayout( | ||
// First argument: the body; component or string or stream | ||
loadPost(getParam('postId')).then( | ||
loadPost(params.postId).then( | ||
post => PostView({ model: post }) | ||
@@ -84,0 +83,0 @@ ), |
{ | ||
"name": "quinn", | ||
"version": "2.0.0-pre5", | ||
"version": "2.0.0-pre6", | ||
"description": "Maybe coming some day.", | ||
@@ -16,7 +16,10 @@ "main": "dist/quinn.js", | ||
"dependencies": { | ||
"bluebird": "^2.1.1", | ||
"bluebird": "^2.1.2", | ||
"continuation-local-storage": "^3.0.0", | ||
"cookie": "^0.1.2", | ||
"debug": "^1.0.2", | ||
"lodash": "^2.4.1", | ||
"minimist": "^0.1.0" | ||
"minimist": "^0.1.0", | ||
"quinn-respond": "^1.0.0", | ||
"resolve-deep": "^1.0.1" | ||
}, | ||
@@ -26,2 +29,4 @@ "devDependencies": { | ||
"mocha": "^1.20.1", | ||
"quinn-router": "^1.0.1", | ||
"quinnc": "^1.0.0", | ||
"reakt": "0.0.5" | ||
@@ -28,0 +33,0 @@ }, |
Maybe coming some day. | ||
### Request handler | ||
*This is what others might call an "application".* | ||
*But "application is a pretty silly name.* | ||
```js | ||
function(request, params) { | ||
return response; | ||
} | ||
``` | ||
#### `request` | ||
A node.js request object, as documented in the offical node.js docs. | ||
There are no additional properties, no magical extension methods. | ||
#### `params` | ||
Think: the "arguments". | ||
This can be path parameters, query parameters. | ||
`params` should never contain anything other than simple data. | ||
By default `params` defaults to an empty object. | ||
If a request handler forwards to a different handler, | ||
it should forward all params that were passed in. | ||
Injecting magic catch-all params is highly discouraged. | ||
Say no to `params.cookies` or `.session` or any other funny business. | ||
`params` SHOULD be a simple object. | ||
Any other kind of value should be the exception. | ||
#### `response` | ||
Describes the response that should be returned. | ||
A proper response has a method to pipe to node.js response stream. | ||
It's also valid to return any of the things | ||
that can be coerced into a response object by `quinn.respond`. |
@@ -36,6 +36,11 @@ # quinn 2 | ||
### 2.0.0-pre6 | ||
* Get rid of bundled modules, split up | ||
## Now | ||
### 2.0.0-pre6 | ||
### 2.0.0-pre7 | ||
* Rendering HTML pages with React integration |
@@ -43,3 +43,3 @@ 'use strict'; | ||
if (argv.v || argv.version) { | ||
return console.log(require('../package.json').version); | ||
return console.log(quinn.version); | ||
} | ||
@@ -46,0 +46,0 @@ |
@@ -18,1 +18,9 @@ 'use strict'; | ||
} | ||
export function lazyCalcForRequest(key, fn, req) { | ||
var value = getFromRequestContext(key); | ||
if (value === undefined) { | ||
setInRequestContext(key, value = fn(req)); | ||
} | ||
return value; | ||
} |
@@ -7,12 +7,20 @@ 'use strict'; | ||
} from 'cookie'; | ||
import {partial} from 'lodash'; | ||
import {lazyCalcForRequest} from './context'; | ||
function _getCookies(req) { | ||
var cookieHeader = req.headers.cookie; | ||
return ( | ||
typeof cookieHeader !== 'string' ? {} | ||
: parseCookies(req.headers.cookie) | ||
); | ||
} | ||
var getCookies = partial(lazyCalcForRequest, 'cookies', _getCookies); | ||
export {getCookies}; | ||
export function getCookie(req, name) { | ||
var cookieHeader = req.headers.cookie; | ||
if (!req._parsedCookies) { | ||
req._parsedCookies = ( | ||
typeof cookieHeader !== 'string' ? {} | ||
: parseCookies(req.headers.cookie) | ||
); | ||
} | ||
var cookies = req._parsedCookies; | ||
var cookies = getCookies(req); | ||
return cookies[name]; | ||
@@ -19,0 +27,0 @@ } |
'use strict'; | ||
import Promise from 'bluebird'; | ||
import {partial} from 'lodash'; | ||
import resolveDeep from 'resolve-deep'; | ||
import Debug from 'debug'; | ||
import respond from 'quinn.respond'; | ||
import respond from 'quinn-respond'; | ||
import {notFound, internalServerError} from 'quinn-respond'; | ||
import {getRequestContextNS} from './context'; | ||
var debug = Debug('quinn:core'); | ||
function pipeTo(target, src) { | ||
@@ -17,24 +21,13 @@ if (src === undefined) return; | ||
function defaultErrorHandler(req, err) { | ||
if (err) { | ||
return ServerError(err.stack); | ||
} else { | ||
return NotFound(`Cannot ${req.method} ${req.url}\n`); | ||
} | ||
return err ? | ||
internalServerError(err.stack) : | ||
notFound(`Cannot ${req.method} ${req.url}\n`); | ||
} | ||
function defaultFatalHandler(req, err) { | ||
setTimeout(() => { | ||
throw err; | ||
}); | ||
setTimeout(() => { throw err; }); | ||
} | ||
function callIfUndefined(fn, value) { | ||
if (value === undefined) { | ||
return fn(); | ||
} else { | ||
return value; | ||
} | ||
} | ||
export default function quinn(handler, errorHandler, fatalHandler) { | ||
function quinn(handler, errorHandler, fatalHandler) { | ||
debug('init'); | ||
if (typeof errorHandler !== 'function') { | ||
@@ -48,29 +41,21 @@ errorHandler = undefined; | ||
return getRequestContextNS().bind(function handleRequest(req, res, pass) { | ||
var hasPass = typeof pass === 'function'; | ||
return getRequestContextNS().bind(function handleRequest(req, res) { | ||
var url = req.url; | ||
debug('handleRequest', url); | ||
var gracefulError = ( | ||
(errorHandler && partial(errorHandler, req)) || | ||
(!hasPass && partial(defaultErrorHandler, req)) || | ||
undefined | ||
partial(defaultErrorHandler, req) | ||
); | ||
var gracefulRespond = ( | ||
(gracefulError && partial(callIfUndefined, gracefulError)) || | ||
undefined | ||
); | ||
function gracefulRespond(res) { | ||
debug('found? %j', res !== undefined, url); | ||
return res === undefined ? gracefulError() : res; | ||
} | ||
var forward = ( | ||
(hasPass && partial(callIfUndefined, pass)) || | ||
undefined | ||
); | ||
Promise.try(handler, [req]) | ||
.then( | ||
gracefulRespond, | ||
gracefulError | ||
) | ||
runRequestHandlerRaw(handler, req, {}) | ||
.then(gracefulRespond, gracefulError) | ||
.then(respond) | ||
.then(r => r.resolved()) | ||
.then(partial(pipeTo, res)) | ||
.then(forward, pass) | ||
.catch(partial(fatalHandler, req)) | ||
@@ -80,9 +65,12 @@ .catch(partial(defaultFatalHandler, req)); | ||
} | ||
quinn.version = require('../package.json').version; | ||
export default quinn; | ||
export function ServerError(props) { | ||
return respond(props).status(500); | ||
export function runRequestHandlerRaw(handler, req, params) { | ||
return resolveDeep(params).then(partial(handler, req)); | ||
} | ||
export function NotFound(props) { | ||
return respond(props).status(404); | ||
export function runRequestHandler(handler, req, params) { | ||
return runRequestHandlerRaw(handler, req, params) | ||
.then(respond); | ||
} |
@@ -1,2 +0,2 @@ | ||
/*global describe, it, after, xit */ | ||
/*global describe, it, after */ | ||
'use strict'; | ||
@@ -11,8 +11,6 @@ | ||
var router = require('quinn.router'); | ||
var router = require('quinn-router'); | ||
var route = router.route; | ||
var getParam = router.getParam; | ||
var getQuery = router.getQuery; | ||
var respond = require('quinn.respond'); | ||
var respond = require('quinn-respond'); | ||
@@ -30,12 +28,17 @@ var Cookies = require('../dist/cookies'); | ||
server = http.createServer(quinn(route(function(app) { | ||
app.GET('/test', function(req) { | ||
app.GET('/test', function(req, params) { | ||
var myCookie = getCookie(req, 'foo'); | ||
return getQuery('a') + ' ' + myCookie; | ||
return params.testParam + ' ' + myCookie; | ||
}, function(req, parsedUrl) { | ||
return { testParam: parsedUrl.query.a }; | ||
}); | ||
app.GET('/throws', function() { | ||
throw new Error('Fatality'); | ||
}); | ||
app.GET('/hello/{name}', function() { | ||
return respond('Hello, ' + getParam('name') + '!').status(201); | ||
app.GET('/hello/{name}', function(req, params) { | ||
return respond('Hello, ' + params.name + '!').status(201); | ||
}); | ||
app.GET('/cookie', function() { | ||
@@ -42,0 +45,0 @@ return setCookie(respond('ok'), 'name', 'jane', { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22786
38
8
5
560
+ Addeddebug@^1.0.2
+ Addedquinn-respond@^1.0.0
+ Addedresolve-deep@^1.0.1
+ Addedcaseless@0.3.0(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddebug@1.0.5(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedms@2.0.0(transitive)
+ Addedquinn-respond@1.0.1(transitive)
+ Addedreadable-stream@1.1.14(transitive)
+ Addedresolve-deep@1.0.1(transitive)
+ Addedstring_decoder@0.10.31(transitive)
Updatedbluebird@^2.1.2