[Pkg-javascript-commits] [node-vary] 01/01: Imported Upstream version 0.1.0

Leo Iannacone l3on-guest at moszumanska.debian.org
Sat Jun 14 15:29:45 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-vary.

commit 27330dfd9c291199f4d54a38135cddf00c0ea306
Author: Leo Iannacone <l3on at ubuntu.com>
Date:   Sat Jun 14 17:18:49 2014 +0200

    Imported Upstream version 0.1.0
---
 .npmignore   |   3 +
 .travis.yml  |  11 ++++
 History.md   |   9 +++
 LICENSE      |  22 +++++++
 README.md    |  43 ++++++++++++++
 index.js     |  79 +++++++++++++++++++++++++
 package.json |  26 +++++++++
 test/test.js | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 381 insertions(+)

diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..cd39b77
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,3 @@
+coverage/
+test/
+.travis.yml
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..1ff243c
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,11 @@
+language: node_js
+node_js:
+  - "0.8"
+  - "0.10"
+  - "0.11"
+matrix:
+  allow_failures:
+    - node_js: "0.11"
+  fast_finish: true
+script: "npm run-script test-travis"
+after_script: "npm install coveralls at 2.10.0 && cat ./coverage/lcov.info | coveralls"
diff --git a/History.md b/History.md
new file mode 100644
index 0000000..10fb1db
--- /dev/null
+++ b/History.md
@@ -0,0 +1,9 @@
+0.1.0 / 2014-06-05
+==================
+
+  * Support array of fields to set
+
+0.0.0 / 2014-06-04
+==================
+
+  * Initial release
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..6ed2825
--- /dev/null
+++ b/README.md
@@ -0,0 +1,43 @@
+# vary
+
+[![NPM version](https://badge.fury.io/js/vary.svg)](http://badge.fury.io/js/vary)
+[![Build Status](https://travis-ci.org/expressjs/vary.svg?branch=master)](https://travis-ci.org/expressjs/vary)
+[![Coverage Status](https://img.shields.io/coveralls/expressjs/vary.svg?branch=master)](https://coveralls.io/r/expressjs/vary)
+
+Update the Vary header of a response
+
+## Install
+
+```sh
+$ npm install vary
+```
+
+## API
+
+```js
+var vary = require('vary')
+```
+
+### vary(res, field)
+
+Adds the given header `field` to the `Vary` response header of `res`.
+This can be a string of a single field or an array of multiple fields.
+
+This will append the header if not already listed, otherwise leaves
+it listed in the current location.
+
+```js
+vary(res, 'Origin')
+vary(res, 'User-Agent')
+vary(res, ['Accept', 'Accept-Language', 'Accept-Encoding'])
+```
+
+## Testing
+
+```sh
+$ npm test
+```
+
+## License
+
+[MIT](LICENSE)
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..6f52730
--- /dev/null
+++ b/index.js
@@ -0,0 +1,79 @@
+/*!
+ * vary
+ * Copyright(c) 2014 Douglas Christopher Wilson
+ * MIT Licensed
+ */
+
+/**
+ * Module exports.
+ */
+
+module.exports = vary;
+
+/**
+ * Variables.
+ */
+
+var separators = /[\(\)<>@,;:\\"\/\[\]\?=\{\}\u0020\u0009]/;
+
+/**
+ * Mark that a request is varied on a header field.
+ *
+ * @param {Object} res
+ * @param {String|Array} field
+ * @api public
+ */
+
+function vary(res, field) {
+  if (!res || !res.getHeader || !res.setHeader) {
+    // quack quack
+    throw new TypeError('res argument is required');
+  }
+
+  if (!field) {
+    throw new TypeError('field argument is required');
+  }
+
+  var fields = !Array.isArray(field)
+    ? [String(field)]
+    : field;
+
+  for (var i = 0; i < fields.length; i++) {
+    if (separators.test(fields[i])) {
+      throw new TypeError('field argument contains an invalid header');
+    }
+  }
+
+  var val = res.getHeader('Vary') || ''
+  var headers = Array.isArray(val)
+    ? val.join(', ')
+    : String(val);
+
+  // existing unspecified vary
+  if (headers === '*') {
+    return;
+  }
+
+  // enumerate current values
+  var vals = headers.toLowerCase().split(/ *, */);
+
+  // unspecified vary
+  if (fields.indexOf('*') !== -1 || vals.indexOf('*') !== -1) {
+    res.setHeader('Vary', '*');
+    return;
+  }
+
+  for (var i = 0; i < fields.length; i++) {
+    field = fields[i].toLowerCase();
+
+    // append value (case-preserving)
+    if (vals.indexOf(field) === -1) {
+      vals.push(field);
+      headers = headers
+        ? headers + ', ' + fields[i]
+        : fields[i];
+    }
+  }
+
+  res.setHeader('Vary', headers);
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..ad09ef1
--- /dev/null
+++ b/package.json
@@ -0,0 +1,26 @@
+{
+  "name": "vary",
+  "description": "Update the Vary header of a response",
+  "version": "0.1.0",
+  "author": "Douglas Christopher Wilson <doug at somethingdoug.com>",
+  "license": "MIT",
+  "keywords": [
+    "http",
+    "res",
+    "vary"
+  ],
+  "repository": "expressjs/vary",
+  "devDependencies": {
+    "istanbul": "0.2.10",
+    "mocha": "~1.20.0",
+    "should": "~4.0.0"
+  },
+  "engines": {
+    "node": ">= 0.8.0"
+  },
+  "scripts": {
+    "test": "mocha --reporter dot test/",
+    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot test/",
+    "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec test/"
+  }
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..c2cb383
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,188 @@
+
+var vary = require('..');
+var should = require('should');
+
+describe('vary(res, header)', function () {
+  describe('arguments', function () {
+    describe('res', function () {
+      it('should be required', function () {
+        vary.bind().should.throw(/res.*required/);
+      });
+
+      it('should not allow non-res-like objects', function () {
+        vary.bind(null, {}).should.throw(/res.*required/);
+      });
+    });
+
+    describe('field', function () {
+      it('should be required', function () {
+        var res = createRes();
+        vary.bind(null, res).should.throw(/field.*required/);
+      });
+
+      it('should accept string', function () {
+        var res = createRes();
+        vary.bind(null, res, 'foo').should.not.throw();
+      });
+
+      it('should accept array of string', function () {
+        var res = createRes();
+        vary.bind(null, res, ['foo', 'bar']).should.not.throw();
+      });
+
+      it('should not allow separators', function () {
+        var res = createRes();
+        vary.bind(null, res, 'invalid:header').should.throw(/field.*contains.*invalid/);
+        vary.bind(null, res, 'invalid header').should.throw(/field.*contains.*invalid/);
+        vary.bind(null, res, ['invalid header']).should.throw(/field.*contains.*invalid/);
+      });
+    });
+  });
+
+  describe('when no Vary', function () {
+    it('should set value', function () {
+      var res = createRes();
+      vary(res, 'Origin');
+      res.getHeader('Vary').should.equal('Origin');
+    });
+
+    it('should set value with multiple calls', function () {
+      var res = createRes();
+      vary(res, 'Origin');
+      vary(res, 'User-Agent');
+      res.getHeader('Vary').should.equal('Origin, User-Agent');
+    });
+
+    it('should preserve case', function () {
+      var res = createRes();
+      vary(res, 'ORIGIN');
+      vary(res, 'user-agent');
+      vary(res, 'AccepT');
+      res.getHeader('Vary').should.equal('ORIGIN, user-agent, AccepT');
+    });
+  });
+
+  describe('when existing Vary', function () {
+    it('should set value', function () {
+      var res = createRes({'vary': 'Accept'});
+      vary(res, 'Origin');
+      res.getHeader('Vary').should.equal('Accept, Origin');
+    });
+
+    it('should set value with multiple calls', function () {
+      var res = createRes({'vary': 'Accept'});
+      vary(res, 'Origin');
+      vary(res, 'User-Agent');
+      res.getHeader('Vary').should.equal('Accept, Origin, User-Agent');
+    });
+
+    it('should not duplicate existing value', function () {
+      var res = createRes({'vary': 'Accept'});
+      vary(res, 'Accept');
+      res.getHeader('Vary').should.equal('Accept');
+    });
+
+    it('should compare case-insensitive', function () {
+      var res = createRes({'vary': 'Accept'});
+      vary(res, 'accEPT');
+      res.getHeader('Vary').should.equal('Accept');
+    });
+
+    it('should preserve case', function () {
+      var res = createRes({'vary': 'Accept'});
+      vary(res, 'AccepT');
+      res.getHeader('Vary').should.equal('Accept');
+    });
+  });
+
+  describe('when existing Vary as array', function () {
+    it('should set value', function () {
+      var res = createRes({'vary': ['Accept', 'Accept-Encoding']});
+      vary(res, 'Origin');
+      res.getHeader('Vary').should.equal('Accept, Accept-Encoding, Origin');
+    });
+
+    it('should not duplicate existing value', function () {
+      var res = createRes({'vary': ['Accept', 'Accept-Encoding']});
+      vary(res, 'accept');
+      vary(res, 'origin');
+      res.getHeader('Vary').should.equal('Accept, Accept-Encoding, origin');
+    });
+  });
+
+  describe('when Vary: *', function () {
+    it('should set value', function () {
+      var res = createRes();
+      vary(res, '*');
+      res.getHeader('Vary').should.equal('*');
+    });
+
+    it('should act as if all values alread set', function () {
+      var res = createRes({'vary': '*'});
+      vary(res, 'Origin');
+      vary(res, 'User-Agent');
+      res.getHeader('Vary').should.equal('*');
+    });
+
+    it('should erradicate existing values', function () {
+      var res = createRes({'vary': 'Accept, Accept-Encoding'});
+      vary(res, '*');
+      res.getHeader('Vary').should.equal('*');
+    });
+
+    it('should update bad existing header', function () {
+      var res = createRes({'vary': 'Accept, Accept-Encoding, *'});
+      vary(res, 'Origin');
+      res.getHeader('Vary').should.equal('*');
+    });
+  });
+
+  describe('when fields is array', function () {
+    it('should set value', function () {
+      var res = createRes();
+      vary(res, ['Accept', 'Accept-Language']);
+      res.getHeader('Vary').should.equal('Accept, Accept-Language');
+    });
+
+    it('should ignore double-entries', function () {
+      var res = createRes();
+      vary(res, ['Accept', 'Accept']);
+      res.getHeader('Vary').should.equal('Accept');
+    });
+
+    it('should be case-insensitive', function () {
+      var res = createRes();
+      vary(res, ['Accept', 'ACCEPT']);
+      res.getHeader('Vary').should.equal('Accept');
+    });
+
+    it('should handle contained *', function () {
+      var res = createRes();
+      vary(res, ['Origin', 'User-Agent', '*', 'Accept']);
+      res.getHeader('Vary').should.equal('*');
+    });
+
+    it('should handle existing values', function () {
+      var res = createRes({'vary': 'Accept, Accept-Encoding'});
+      vary(res, ['origin', 'accept', 'accept-charset']);
+      res.getHeader('Vary').should.equal('Accept, Accept-Encoding, origin, accept-charset');
+    });
+  });
+});
+
+function createRes(headers) {
+  var _headers = {};
+
+  for (var key in headers) {
+    _headers[key.toLowerCase()] = headers[key];
+  }
+
+  return {
+    getHeader: function (name) {
+      return _headers[name.toLowerCase()];
+    },
+    setHeader: function (name, val) {
+      _headers[name.toLowerCase()] = val;
+    }
+  };
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-vary.git



More information about the Pkg-javascript-commits mailing list