[Pkg-javascript-commits] [node-on-headers] 02/06: Imported Upstream version 1.0.0
Leo Iannacone
l3on-guest at moszumanska.debian.org
Fri Oct 10 14:15:42 UTC 2014
This is an automated email from the git hooks/post-receive script.
l3on-guest pushed a commit to branch master
in repository node-on-headers.
commit 3050f6d525d4c63ec74ec074561a998293fb65ea
Author: Leo Iannacone <l3on at ubuntu.com>
Date: Fri Oct 10 13:32:42 2014 +0200
Imported Upstream version 1.0.0
---
.npmignore | 1 +
.travis.yml | 2 +
History.md | 9 +++-
README.md | 37 +++++---------
index.js | 27 ++++++++--
package.json | 19 +++----
test/test.js | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
7 files changed, 212 insertions(+), 43 deletions(-)
diff --git a/.npmignore b/.npmignore
index ac0bfb9..cd39b77 100644
--- a/.npmignore
+++ b/.npmignore
@@ -1,2 +1,3 @@
+coverage/
test/
.travis.yml
diff --git a/.travis.yml b/.travis.yml
index 65cf4bc..bdd4e73 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,3 +7,5 @@ matrix:
allow_failures:
- node_js: "0.11"
fast_finish: true
+script: "npm run-script test-travis"
+after_script: "test $TRAVIS_NODE_VERSION = '0.10' && npm install coveralls at 2.11.1 && cat ./coverage/lcov.info | coveralls"
diff --git a/History.md b/History.md
index 369468c..0e5dc11 100644
--- a/History.md
+++ b/History.md
@@ -1,4 +1,11 @@
+1.0.0 / 2014-08-10
+==================
+
+ * Honor `res.statusCode` change in `listener`
+ * Move to `jshttp` orgainzation
+ * Prevent `arguments`-related de-opt
+
0.0.0 / 2014-05-13
==================
- * Genesis from `connect`
+ * Initial implementation
diff --git a/README.md b/README.md
index 3b9bdd4..627ee93 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,10 @@
-# on-headers [![Build Status](https://travis-ci.org/expressjs/on-headers.svg)](https://travis-ci.org/expressjs/on-headers) [![NPM version](https://badge.fury.io/js/on-headers.svg)](http://badge.fury.io/js/on-headers)
+# on-headers
+
+[![NPM Version](https://img.shields.io/npm/v/on-headers.svg?style=flat)](https://www.npmjs.org/package/on-headers)
+[![Node.js Version](https://img.shields.io/badge/node.js->=_0.8-blue.svg?style=flat)](http://nodejs.org/download/)
+[![Build Status](https://img.shields.io/travis/jshttp/on-headers.svg?style=flat)](https://travis-ci.org/jshttp/on-headers)
+[![Coverage Status](https://img.shields.io/coveralls/jshttp/on-headers.svg?style=flat)](https://coveralls.io/r/jshttp/on-headers)
+[![Gittip](https://img.shields.io/gittip/dougwilson.svg?style=flat)](https://www.gittip.com/dougwilson/)
Execute a listener when a response is about to write headers.
@@ -17,8 +23,11 @@ var onHeaders = require('on-headers')
### onHeaders(res, listener)
This will add the listener `listener` to fire when headers are emitted for `res`.
-Headers are considered to be emitted only once, right before they are sent to
-the client.
+The listener is passed the `response` object as it's context (`this`). Headers are
+considered to be emitted only once, right before they are sent to the client.
+
+When this is called multiple times on the same `res`, the `listener`s are fired
+in the reverse order they were added.
## Examples
@@ -47,24 +56,4 @@ function onRequest(req, res) {
## License
-The MIT License (MIT)
-
-Copyright (c) 2014 Douglas Christopher Wilson doug at somethingdoug.com
-
-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.
+[MIT](LICENSE)
diff --git a/index.js b/index.js
index 850e059..8579f8d 100644
--- a/index.js
+++ b/index.js
@@ -19,8 +19,12 @@ var slice = Array.prototype.slice
*/
module.exports = function onHeaders(res, listener) {
+ if (!res) {
+ throw new TypeError('argument res is required')
+ }
+
if (typeof listener !== 'function') {
- throw new TypeError('listener must be a function')
+ throw new TypeError('argument listener must be a function')
}
res.writeHead = createWriteHead(res.writeHead, listener)
@@ -38,6 +42,12 @@ function createWriteHead(prevWriteHead, listener) {
if (!fired) {
fired = true
listener.call(this)
+
+ // pass-along an updated status code
+ if (typeof args[0] === 'number' && this.statusCode !== args[0]) {
+ args[0] = this.statusCode
+ args.length = 1
+ }
}
prevWriteHead.apply(this, args);
@@ -45,11 +55,14 @@ function createWriteHead(prevWriteHead, listener) {
}
function setWriteHeadHeaders(statusCode) {
- var headerIndex = typeof arguments[1] === 'string'
+ var length = arguments.length
+ var headerIndex = length > 1 && typeof arguments[1] === 'string'
? 2
: 1
- var headers = arguments[headerIndex]
+ var headers = length >= headerIndex + 1
+ ? arguments[headerIndex]
+ : undefined
this.statusCode = statusCode
@@ -68,5 +81,11 @@ function setWriteHeadHeaders(statusCode) {
}
}
- return slice.call(arguments, 0, headerIndex)
+ // copy leading arguments
+ var args = new Array(Math.min(length, headerIndex))
+ for (var i = 0; i < args.length; i++) {
+ args[i] = arguments[i]
+ }
+
+ return args
}
diff --git a/package.json b/package.json
index c019f5c..ce354ce 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "on-headers",
"description": "Execute a listener when a response is about to write headers",
- "version": "0.0.0",
+ "version": "1.0.0",
"author": "Douglas Christopher Wilson <doug at somethingdoug.com>",
"license": "MIT",
"keywords": [
@@ -10,22 +10,19 @@
"http",
"onheaders"
],
- "repository": {
- "type": "git",
- "url": "https://github.com/expressjs/on-headers.git"
- },
- "bugs": {
- "url": "https://github.com/expressjs/on-headers/issues"
- },
+ "repository": "jshttp/on-headers",
"dependencies": {},
"devDependencies": {
- "mocha": "~1.18.2",
- "supertest": "~0.12.1"
+ "istanbul": "0.3.0",
+ "mocha": "~1.21.4",
+ "supertest": "~0.13.0"
},
"engines": {
"node": ">= 0.8.0"
},
"scripts": {
- "test": "mocha --reporter spec test/"
+ "test": "mocha --reporter spec --bail --check-leaks test/",
+ "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
+ "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
}
}
diff --git a/test/test.js b/test/test.js
index 6fe167b..d050354 100644
--- a/test/test.js
+++ b/test/test.js
@@ -66,6 +66,48 @@ describe('onHeaders(res, listener)', function () {
})
})
+ it('should fire in reverse order', function (done) {
+ var server = createServer(echoListener, handler)
+
+ function handler(req, res) {
+ onHeaders(res, appendHeader(1))
+ onHeaders(res, appendHeader(2))
+ onHeaders(res, appendHeader(3))
+ res.setHeader('X-Outgoing', 'test')
+ }
+
+ request(server)
+ .get('/')
+ .expect('X-Outgoing-Echo', 'test,3,2,1')
+ .expect(200, done)
+ })
+
+ describe('arguments', function () {
+ describe('res', function () {
+ it('should be required', function () {
+ assert.throws(onHeaders.bind(), /res.*required/)
+ })
+ })
+
+ describe('listener', function () {
+ it('should be required', function (done) {
+ var server = createServer()
+
+ request(server)
+ .get('/')
+ .expect(500, /listener.*function/, done)
+ })
+
+ it('should only accept function', function (done) {
+ var server = createServer(42)
+
+ request(server)
+ .get('/')
+ .expect(500, /listener.*function/, done)
+ })
+ })
+ })
+
describe('setHeader', function () {
it('should be available in listener', function (done) {
var server = createServer(echoListener)
@@ -77,6 +119,86 @@ describe('onHeaders(res, listener)', function () {
})
})
+ describe('writeHead(status)', function () {
+ it('should make status available in listener', function (done) {
+ var server = createServer(listener, handler)
+
+ function handler(req, res) {
+ res.writeHead(201)
+ }
+
+ function listener(req, res) {
+ this.setHeader('X-Status', this.statusCode)
+ }
+
+ request(server)
+ .get('/')
+ .expect('X-Status', '201')
+ .expect(201, done)
+ })
+
+ it('should allow manipulation of status in listener', function (done) {
+ var server = createServer(listener, handler)
+
+ function handler(req, res) {
+ res.writeHead(201)
+ }
+
+ function listener(req, res) {
+ this.setHeader('X-Status', this.statusCode)
+ this.statusCode = 202
+ }
+
+ request(server)
+ .get('/')
+ .expect('X-Status', '201')
+ .expect(202, done)
+ })
+
+ it('should pass-through core error', function (done) {
+ var server = createServer(echoListener, handler)
+
+ function handler(req, res) {
+ res.writeHead() // error
+ }
+
+ request(server)
+ .get('/')
+ .expect(500, done)
+ })
+ })
+
+ describe('writeHead(status, reason)', function () {
+ it('should be available in listener', function (done) {
+ var server = createServer(echoListener, handler)
+
+ function handler(req, res) {
+ res.setHeader('X-Outgoing', 'test')
+ res.writeHead(200, 'OK')
+ }
+
+ request(server)
+ .get('/')
+ .expect('X-Outgoing-Echo', 'test')
+ .expect(200, done)
+ })
+ })
+
+ describe('writeHead(status, reason, obj)', function () {
+ it('should be available in listener', function (done) {
+ var server = createServer(echoListener, handler)
+
+ function handler(req, res) {
+ res.writeHead(200, 'OK', {'X-Outgoing': 'test'})
+ }
+
+ request(server)
+ .get('/')
+ .expect('X-Outgoing-Echo', 'test')
+ .expect(200, done)
+ })
+ })
+
describe('writeHead(status, obj)', function () {
it('should be available in listener', function (done) {
var server = createServer(listener, handler)
@@ -96,6 +218,25 @@ describe('onHeaders(res, listener)', function () {
.expect('X-Outgoing-Echo', 'test')
.expect(201, done)
})
+
+ it('should handle falsy keys', function (done) {
+ var server = createServer(listener, handler)
+
+ function handler(req, res) {
+ res.writeHead(201, {'X-Outgoing': 'test', '': 'test'})
+ }
+
+ function listener(req, res) {
+ this.setHeader('X-Status', this.statusCode)
+ this.setHeader('X-Outgoing-Echo', this.getHeader('X-Outgoing'))
+ }
+
+ request(server)
+ .get('/')
+ .expect('X-Status', '201')
+ .expect('X-Outgoing-Echo', 'test')
+ .expect(201, done)
+ })
})
describe('writeHead(status, arr)', function () {
@@ -124,12 +265,25 @@ function createServer(listener, handler) {
handler = handler || echoHandler
return http.createServer(function (req, res) {
- onHeaders(res, listener)
- handler(req, res)
- res.end()
+ try {
+ onHeaders(res, listener)
+ handler(req, res)
+ res.statusCode = 200
+ } catch (err) {
+ res.statusCode = 500
+ res.write(err.message)
+ } finally {
+ res.end()
+ }
})
}
+function appendHeader(num) {
+ return function onHeaders() {
+ this.setHeader('X-Outgoing', this.getHeader('X-Outgoing') + ',' + num)
+ }
+}
+
function echoHandler(req, res) {
res.setHeader('X-Outgoing', 'test')
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-on-headers.git
More information about the Pkg-javascript-commits
mailing list