Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: npm/npm-www
...
head fork: npm/npm-www
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 126 additions and 111 deletions.
  1. +115 −0 decorate.js
  2. +4 −2 routes/login.js
  3. +7 −109 site.js
View
115 decorate.js
@@ -0,0 +1,115 @@
+
+module.exports = decorate
+
+var errors = require("./errors.js")
+, domain = require("domain")
+, Cookies = require("cookies")
+, Negotiator = require("negotiator")
+, RedSess = require("redsess")
+
+, path = require('path')
+, Templar = require("templar")
+, ejs = require('ejs')
+, tplDir = path.resolve(__dirname, 'templates')
+, templateOptions = { engine: ejs, folder: tplDir }
+, url = require('url')
+
+, CouchLogin = require('couch-login')
+
+function decorate (req, res, config) {
+ templateOptions.debug = config.debug
+
+ // handle unexpected errors relating to this request.
+ var d = domain.create()
+ d.add(req)
+ d.add(res)
+ d.on("error", function (er) {
+ try {
+ res.error(er)
+ // don't destroy before sending the error
+ res.on("close", function () {
+ d.dispose()
+ })
+
+ // don't wait forever, though.
+ setTimeout(function () {
+ d.dispose()
+ }, 1000)
+
+ } catch (er) {
+ d.dispose()
+ }
+ })
+
+ // set up various decorations
+ // TODO: Move some/all of this into a separate module.
+
+ req.cookies = res.cookies = new Cookies(req, res, config.keys)
+ req.negotiator = new Negotiator(req)
+ req.neg = req.negotiator
+ req.session = res.session = new RedSess(req, res)
+
+ // set up the CouchLogin to automatically save the token in the
+ // session, and log in on demand.
+ req.couch = CouchLogin(config.registryCouch).decorate(req, res)
+
+ res.template = Templar(req, res, templateOptions)
+
+ // don't print out that dumb 'cannot send blah blah' message
+ if (req.method === 'HEAD') {
+ res.write = (function (o) {
+ return function (c) { return o.call(res, '') }
+ })(res.write)
+ res.end = (function (o) {
+ return function (c) { return o.call(res) }
+ })(res.end)
+ }
+
+ // allow stuff like "req.pathname", etc.
+ var u = url.parse(req.url)
+ delete u.auth
+ Object.keys(u, true).forEach(function (k) {
+ req[k] = u[k]
+ })
+
+ res.error = function (er, code) {
+ if (code && typeof code === 'number') {
+ if (er) er.statusCode = code
+ else er = code
+ }
+ errors(er, req, res)
+ }
+
+ res.redirect = function (target, code) {
+ res.statusCode = code || 302
+ res.setHeader('location', target)
+ var avail = ['text/html', 'application/json']
+ var mt = req.neg.preferredMediaType(avail)
+ if (mt === 'application/json') {
+ res.sendJSON({ redirect: target, statusCode: code })
+ } else {
+ res.sendHTML('<html><body><h1>Moved'
+ + (code === 302 ? ' Permanently' : '') + '</h1>'
+ +'<a href="' + target + '">' + target + '</a>')
+ }
+ }
+
+ res.send = function (data, status, headers) {
+ res.statusCode = res.statusCode || status
+ if (headers) Object.keys(headers).forEach(function (h) {
+ res.setHeader(h, headers[h])
+ })
+ if (!Buffer.isBuffer(data)) data = new Buffer(data)
+ res.setHeader('content-length', data.length)
+ res.end(data)
+ }
+
+
+ res.sendJSON = function (obj, status) {
+ res.send(JSON.stringify(obj), status, {'content-type':'application/json'})
+ }
+
+ res.sendHTML = function (data, status) {
+ res.send(data, status, {'content-type':'text/html'})
+ }
+}
View
6 routes/login.js
@@ -11,7 +11,7 @@ function login (req, res) {
return res.error(new Error('bad login'), 400)
}
- req.couch.login(data, function (er, cr, data) {
+ req.couch.login(data, function (er, cr, couchSession) {
if (er) return res.error(er)
if (cr.statusCode !== 200) {
// XXX Should just render the login form
@@ -25,7 +25,9 @@ function login (req, res) {
// it anyway.
var pu = '/_users/org.couchdb.user:' + data.name
req.couch.get(pu, function (er, cr, data) {
- if (er) return res.error(er, cr && cr.statusCode)
+ if (er || cr.statusCode !== 200) {
+ return res.error(er, cr && cr.statusCode)
+ }
req.session.set("profile", data)
View
116 site.js
@@ -3,122 +3,21 @@
module.exports = site
var router = require("./router.js")
-, errors = require("./errors.js")
-, domain = require("domain")
+, decorate = require('./decorate.js')
, config = require("./config.js")
-, Cookies = require("cookies")
+
, Keygrip = require("keygrip")
-, keys = new Keygrip(config.keys)
-, Negotiator = require("negotiator")
-, RedSess = require("redsess")
-, url = require("url")
+
, StringDecoder = require('string_decoder').StringDecoder
, qs = require("querystring")
+, RedSess = require("redsess")
-, path = require('path')
-, Templar = require("templar")
-, ejs = require('ejs')
-, tplDir = path.resolve(__dirname, 'templates')
-, templateOptions = { engine: ejs, debug: config.debug, folder: tplDir }
-
-, CouchLogin = require('couch-login')
+config.keys = new Keygrip(config.keys)
RedSess.createClient(config.redis)
function site (req, res) {
- // handle unexpected errors relating to this request.
- var d = domain.create()
- d.add(req)
- d.add(res)
- d.on("error", function (er) {
- try {
- res.error(er)
- // don't destroy before sending the error
- res.on("close", function () {
- d.dispose()
- })
-
- // don't wait forever, though.
- setTimeout(function () {
- d.dispose()
- }, 1000)
-
- } catch (er) {
- d.dispose()
- }
- })
-
- // set up various decorations
- // TODO: Move some/all of this into a separate module.
-
- req.cookies = res.cookies = new Cookies(req, res, keys)
- req.negotiator = new Negotiator(req)
- req.neg = req.negotiator
- req.session = res.session = new RedSess(req, res)
-
- // set up the CouchLogin to automatically save the token in the
- // session, and log in on demand.
- req.couch = CouchLogin(config.registryCouch).decorate(req, res)
-
- res.template = Templar(req, res, templateOptions)
-
- // don't print out that dumb 'cannot send blah blah' message
- if (req.method === 'HEAD') {
- res.write = (function (o) {
- return function (c) { return o.call(res, '') }
- })(res.write)
- res.end = (function (o) {
- return function (c) { return o.call(res) }
- })(res.end)
- }
-
- // allow stuff like "req.pathname", etc.
- var u = url.parse(req.url)
- delete u.auth
- Object.keys(u, true).forEach(function (k) {
- req[k] = u[k]
- })
-
- res.error = function (er, code) {
- if (code && typeof code === 'number') er.statusCode = code
- errors(er, req, res)
- }
-
- res.redirect = function (target, code) {
- res.statusCode = code || 302
- res.setHeader('location', target)
- var avail = ['text/html', 'application/json']
- var mt = req.neg.preferredMediaType(avail)
- if (mt === 'application/json') {
- res.sendJSON({ redirect: target, statusCode: code })
- } else {
- res.sendHTML('<html><body><h1>Moved'
- + (code === 302 ? ' Permanently' : '') + '</h1>'
- +'<a href="' + target + '">' + target + '</a>')
- }
- }
-
- res.send = function (data, status, headers) {
- res.statusCode = res.statusCode || status
- if (headers) Object.keys(headers).forEach(function (h) {
- res.setHeader(h, headers[h])
- })
- if (!Buffer.isBuffer(data)) data = new Buffer(data)
- res.setHeader('content-length', data.length)
- res.end(data)
- }
-
-
- res.sendJSON = function (obj, status) {
- res.send(JSON.stringify(obj), status, {'content-type':'application/json'})
- }
-
- res.sendHTML = function (data, status) {
- res.send(data, status, {'content-type':'text/html'})
- }
- // decoration over
-
-
+ decorate(req, res, config)
var route = router.match(req.url)
if (!route) return res.error(404)
@@ -129,6 +28,7 @@ function site (req, res) {
route.fn(req, res)
+ // now check for anything added afterwards.
if (typeof req.maxLen === 'number') {
var cl = req.headers['content-length']
res.setHeader('max-length', ''+req.maxLen)
@@ -172,6 +72,4 @@ function site (req, res) {
req.emit('body', b)
})
}
-
}
-

No commit comments for this range

Something went wrong with that request. Please try again.