[Pkg-javascript-commits] [node-on-headers] 01/01: Imported Upstream version 0.0.0

Leo Iannacone l3on-guest at moszumanska.debian.org
Sat Jun 14 13:39:59 UTC 2014


This is an automated email from the git hooks/post-receive script.

l3on-guest pushed a commit to annotated tag upstream/0.0.0
in repository node-on-headers.

commit 1d8b6d75da48751d391689c68cac3bf3b19632da
Author: Leo Iannacone <l3on at ubuntu.com>
Date:   Sat Jun 14 15:30:46 2014 +0200

    Imported Upstream version 0.0.0
---
 .npmignore   |   2 +
 .travis.yml  |   9 ++++
 History.md   |   4 ++
 LICENSE      |  22 ++++++++++
 README.md    |  70 ++++++++++++++++++++++++++++++
 index.js     |  72 +++++++++++++++++++++++++++++++
 package.json |  31 +++++++++++++
 test/test.js | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 349 insertions(+)

diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..ac0bfb9
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,2 @@
+test/
+.travis.yml
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..65cf4bc
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,9 @@
+language: node_js
+node_js:
+  - "0.8"
+  - "0.10"
+  - "0.11"
+matrix:
+  allow_failures:
+    - node_js: "0.11"
+  fast_finish: true
diff --git a/History.md b/History.md
new file mode 100644
index 0000000..369468c
--- /dev/null
+++ b/History.md
@@ -0,0 +1,4 @@
+0.0.0 / 2014-05-13
+==================
+
+  * Genesis from `connect`
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..b7dce6c
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2014 Douglas Christopher Wilson
+
+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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..3b9bdd4
--- /dev/null
+++ b/README.md
@@ -0,0 +1,70 @@
+# 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)
+
+Execute a listener when a response is about to write headers.
+
+## Install
+
+```sh
+$ npm install on-headers
+```
+
+## API
+
+```js
+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.
+
+## Examples
+
+```js
+var http = require('http')
+var onHeaders = require('on-headers')
+
+http
+.createServer(onRequest)
+.listen(3000)
+
+function addPoweredBy() {
+  // add if not set by end of request
+  if (!this.getHeader('X-Powered-By')) {
+    this.addHeader('X-Powered-By', 'Node.js')
+  }
+}
+
+function onRequest(req, res) {
+  onHeaders(res, addPoweredBy)
+
+  res.setHeader('Content-Type', 'text/plain')
+  res.end('hello!')
+}
+```
+
+## 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.
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..850e059
--- /dev/null
+++ b/index.js
@@ -0,0 +1,72 @@
+/*!
+ * on-headers
+ * Copyright(c) 2014 Douglas Christopher Wilson
+ * MIT Licensed
+ */
+
+/**
+ * Reference to Array slice.
+ */
+
+var slice = Array.prototype.slice
+
+/**
+ * Execute a listener when a response is about to write headers.
+ *
+ * @param {Object} res
+ * @return {Function} listener
+ * @api public
+ */
+
+module.exports = function onHeaders(res, listener) {
+  if (typeof listener !== 'function') {
+    throw new TypeError('listener must be a function')
+  }
+
+  res.writeHead = createWriteHead(res.writeHead, listener)
+}
+
+function createWriteHead(prevWriteHead, listener) {
+  var fired = false;
+
+  // return function with core name and argument list
+  return function writeHead(statusCode) {
+    // set headers from arguments
+    var args = setWriteHeadHeaders.apply(this, arguments);
+
+    // fire listener
+    if (!fired) {
+      fired = true
+      listener.call(this)
+    }
+
+    prevWriteHead.apply(this, args);
+  }
+}
+
+function setWriteHeadHeaders(statusCode) {
+  var headerIndex = typeof arguments[1] === 'string'
+    ? 2
+    : 1
+
+  var headers = arguments[headerIndex]
+
+  this.statusCode = statusCode
+
+  // the following block is from node.js core
+  if (Array.isArray(headers)) {
+    // handle array case
+    for (var i = 0, len = headers.length; i < len; ++i) {
+      this.setHeader(headers[i][0], headers[i][1])
+    }
+  } else if (headers) {
+    // handle object case
+    var keys = Object.keys(headers)
+    for (var i = 0; i < keys.length; i++) {
+      var k = keys[i]
+      if (k) this.setHeader(k, headers[k])
+    }
+  }
+
+  return slice.call(arguments, 0, headerIndex)
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..c019f5c
--- /dev/null
+++ b/package.json
@@ -0,0 +1,31 @@
+{
+  "name": "on-headers",
+  "description": "Execute a listener when a response is about to write headers",
+  "version": "0.0.0",
+  "author": "Douglas Christopher Wilson <doug at somethingdoug.com>",
+  "license": "MIT",
+  "keywords": [
+    "event",
+    "headers",
+    "http",
+    "onheaders"
+  ],
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/expressjs/on-headers.git"
+  },
+  "bugs": {
+    "url": "https://github.com/expressjs/on-headers/issues"
+  },
+  "dependencies": {},
+  "devDependencies": {
+    "mocha": "~1.18.2",
+    "supertest": "~0.12.1"
+  },
+  "engines": {
+    "node": ">= 0.8.0"
+  },
+  "scripts": {
+    "test": "mocha --reporter spec test/"
+  }
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..6fe167b
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,139 @@
+
+var assert = require('assert')
+var http = require('http')
+var onHeaders = require('..')
+var request = require('supertest')
+
+describe('onHeaders(res, listener)', function () {
+  it('should fire after setHeader', function (done) {
+    var server = createServer(echoListener)
+
+    request(server)
+    .get('/')
+    .expect('X-Outgoing-Echo', 'test')
+    .expect(200, done)
+  })
+
+  it('should fire before write', function (done) {
+    var server = createServer(echoListener, handler)
+
+    function handler(req, res) {
+      res.setHeader('X-Outgoing', 'test')
+      res.write('1')
+    }
+
+    request(server)
+    .get('/')
+    .expect('X-Outgoing-Echo', 'test')
+    .expect(200, '1', done)
+  })
+
+  it('should fire with no headers', function (done) {
+    var server = createServer(listener, handler)
+
+    function handler(req, res) {}
+
+    function listener(req, res) {
+      this.setHeader('X-Headers', Object.keys(this._headers || {}).join(','))
+    }
+
+    request(server)
+    .get('/')
+    .expect('X-Headers', '')
+    .expect(200, done)
+  })
+
+  it('should fire only once', function (done) {
+    var count = 0
+    var server = createServer(listener, handler)
+
+    function handler(req, res) {
+      res.writeHead(200)
+
+      try { res.writeHead(200) } catch (e) {}
+    }
+
+    function listener(req, res) {
+      count++
+    }
+
+    request(server)
+    .get('/')
+    .expect(200, function (err) {
+      if (err) return done(err)
+      assert.equal(count, 1)
+      done()
+    })
+  })
+
+  describe('setHeader', function () {
+    it('should be available in listener', function (done) {
+      var server = createServer(echoListener)
+
+      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)
+
+      function handler(req, res) {
+        res.writeHead(201, {'X-Outgoing': '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 () {
+    it('should be available in listener', function (done) {
+      var server = createServer(listener, handler)
+
+      function handler(req, res) {
+        res.writeHead(201, [['X-Outgoing', '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)
+    })
+  })
+})
+
+function createServer(listener, handler) {
+  handler = handler || echoHandler
+
+  return http.createServer(function (req, res) {
+    onHeaders(res, listener)
+    handler(req, res)
+    res.end()
+  })
+}
+
+function echoHandler(req, res) {
+  res.setHeader('X-Outgoing', 'test')
+}
+
+function echoListener() {
+  this.setHeader('X-Outgoing-Echo', this.getHeader('X-Outgoing'))
+}

-- 
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